Professional Documents
Culture Documents
Jquery Notes
Jquery Notes
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<title>JQuery Ex 1</title>
<style>
table, th, td {
border: 1px solid black;
}
tr.nice td {
background: #FAFAD2; /* This css will be applied when we add a class with name
'nice' dynamically (in jquery) to a tr*/
}
tr.mouseon td {
background: #1E90FF; /* This css will be applied when we add a class with name
'mouseon' dynamically (in jquery) to a tr*/
}
</style>
<script type="text/javascript"
src="http://ajax.googleapis.com/ajax/libs/jquery/1.5/jquery.min.js"></script>
<script type="text/javascript">
$("document").ready(function () {
$("td").css("padding", "6px 10px"); // selecting all td's in the document
$(".hero").css("color", "red");
// similar to above
</p>
</div>
<div>
<h3>The Most Powerful Superhumans</h3>
<ul id="superhumans">
<li class="unknown">Beyonder</li>
<li class="villian">Galactus</li>
<li class="hero">Franklin Richards</li>
<li class="hero">Reed Richards</li>
</ul>
<p>
<h4>Who Do You Think is Most Powerful?</h4>
</p>
<h6>Very Small</h6>
</div>
<div>
<h3 id="randstuff">Stuff 1</h3>
<h4>Stuff 2</h4>
<h5>Stuff 3</h5>
</div>
</body>
</html>
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<title>JQuery Ex 1</title>
<script type="text/javascript"
src="http://ajax.googleapis.com/ajax/libs/jquery/1.5/jquery.min.js"></script>
<script type="text/javascript">
$("document").ready(function () {
$("h4:first").css("background-color", "yellow"); // selects first h4 tag in the document
$("p:last").css({ "background-color": "purple", "color": "white" }); // selects last
paragraph tag in the document. It also shows how to set multiple css at once.
$("p span:first-child").css("background-color", "orange"); // selects first child span inside
a paragraph. (If there is anyelement b/w p and first span then in such case it will not select
first span)
$("div p:last-child").css("background-color", "cyan"); // selects last paragraph in div
$("div:nth-child(1)").css("background-color", "pink"); // it selects exactly the child which
we want to grab. (I did not understand this!)
if ($('#clickToHide').is(':visible')) {
forget to put ':')
//$('#clickToHide').hide();
$('#clickToHide').fadeOut(2000);
$(this).val('Bring Back');
}
else {
//$('#clickToHide').show();
$('#clickToHide').fadeIn(2000);
$(this).val('Delete Text');
});
//
// You can also replace hide & show with fadeIn or fadeOut
//
// // You can play with the speed by giving values in milliseconds or slow // fast, or
normal ex. fadeIn(1000) or fadeOut(slow)
});
</script>
<noscript>
<h3>This site requires JavaScript</h3>
</noscript>
</head>
<body>
<div id="firstDiv">
<h4>First Header</h4>
<p id="firstPara">First paragraph in first div
<span> First Span</span>
<span> Sample Span</span>
</p>
<p>Second paragraph in first div</p>
<div id="secondDiv">
<h4>Second Header</h4>
<p>First paragraph in second div<span> Second Span</span></p>
<p>Second paragraph in second div</p>
<p>Third paragraph in first div</p>
</div>
<div id="thirdDiv">
<h4>Third Header</h4>
<p>First paragraph in third div<span> Third Span</span></p>
<p>Second paragraph in third div</p>
<p>Third paragraph in first div</p>
</div>
<span id="clickToHide">Click to hide me</span>
<input type="button" id="bringItBack" value="Bring Back" />
</body>
</html>
PART 3:
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<title>JQuery Event Handlers</title>
<script type="text/javascript" src="http://code.jquery.com/jquery-latest.js"></script>
<script type="text/javascript">
$("document").ready(function () {
$('#oneButton').bind('click',alertButtonClick);
$('#textBox1').bind('blur',onBlurEvent)
.bind('focus',onFocusEvent)
.bind('onmousedown',onMDownEvent)
.bind('onmouseup',onMUpEvent)
.bind('change',onChangeEvent);
$(window).resize(resizedWindow); // assigning multiple events at once
$('#twoButton').bind('dblclick', dblClickedMe);
$('#logo').bind('mouseover', mouseOverMe).bind('mouseout', mouseOutMe); //
assigning multiple events at once
$("form").submit(function() { alert("Submit button clicked") });
$('#threeButton').bind('click',unbindLogo);
$
('#theBody').bind('keyup',checkKeyPressed).bind('mousemove',theMouseMoved).click(event,
eventTriggered);
});
function alertButtonClick() {
alert("There was a button clicked");
}
function onBlurEvent() {
$("#second").html("You left the text box");
}
function onFocusEvent() {
$("#second").html("You entered the text box");
}
function onMDownEvent() {
$("#second").html("You left the text box");
}
function onMUpEvent() {
$("#second").html("You entered the text box");
}
function onChangeEvent() {
$("#third").html("You changed the text box");
}
function resizedWindow() {
$("#second").html("Window was resized W: " + $(window).width() + " H: " + $
(window).height());
}
function dblClickedMe() {
$("#second").html("You double clicked");
}
function mouseOverMe() {
$("#second").html("You put your cursor on my logo");
}
function mouseOutMe() {
$("#second").html("You left my logo");
}
function unbindLogo() {
$('#logo').unbind('mouseover', mouseOverMe).unbind('mouseout', mouseOutMe); //
It removes mouseover & mouseout events from logo
}
function checkKeyPressed(event) {
$('#fifth').text(String.fromCharCode(event.keyCode)); // gets the alphabet of key
pressed
}
function theMouseMoved(event) {
$("#seventh").html(event.screenX); // gets the x coordinate
$("#ninth").html(event.screenY); // gets the y coordinate
}
function eventTriggered(event) {
$("#tenth").text(event.target.nodeName); // gets the element name on which click
action is performed
$("#eleventh").html(event.timeStamp);
}
</script>
</head>
<body id="theBody">
<div>
<h3>JQuery Event Handlers</h3>
</div>
<div>
<img src='http://www.newthinktank.com/favicon.png' id="logo" alt='Little Brain' />
</div>
<br />
<div>
<p><strong>Events Triggered on the Page:</strong></p>
<p id="second">Waiting for Event</p>
<p id="third">Waiting for Change</p>
</div>
<div>
<p><strong>Key Events Triggered on the Page:</strong></p>
<span id="fourth">Key Pressed:</span>
<span id="fifth">Waiting for Change</span>
</div>
<br />
<div>
<p><strong>Mouse Movements on the Page:</strong></p>
<span id="sixth">Mouse X Coordinate:</span>
<span id="seventh">Waiting for Change</span><br />
<span id="eighth">Mouse Y Coordinate:</span>
<span id="ninth">Waiting for Change</span><br />
<span>Textbox Changed: </span>
<span id="tenth">Waiting for Event</span><br />
<span>Element Changed When: </span>
<span id="eleventh">Event Occurred When</span>
</div>
<br />
<form>
<button type="button" id="oneButton">Alert on Click</button>
<button type="button" id="twoButton">Click on me Twice</button>
<button type="button" id="threeButton">Unbind the Logo</button>
<input type='text' id="textBox1" size="40" />
<input type='submit' value='Send' />
</form>
</body>
</html>
PART 4:
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<title>JQuery Animations</title>
<script type="text/javascript" src="http://code.jquery.com/jquery-latest.js"></script>
<script type="text/javascript">
$("document").ready(function() {
</div>
<br />
<div id="randPara">
<p id="firstP">The first paragraph</p>
</div>
<br />
<form>
<input type="text" id="randText"/>
<button type="button" id="replaceWHtml">Replace w/ HTML</button>
<button type="button" id="replaceWText">Replace w/ Text</button>
<button type="button" id="addAPara">Add Text</button>
<button type="button" id="removeAPara">Remove Text</button>
<button type="button" id="lastIsFirst">Last to First</button>
<button type="button" id="addBefore">Add Before</button>
<button type="button" id="addAfter">Add After</button>
<button type="button" id="addToTextBox">Add To Text Box</button>
</form>
</body>
</html>
PART 5:
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<title>JQuery Animations</title>
<script type="text/javascript" src="http://code.jquery.com/jquery-latest.js"></script>
<script type="text/javascript">
$("document").ready(function () {
$('#deleteSpan').bind('click', deleteSpan);
// before places the new element before the paragraph
$('#randPara').before('<span>Before Paragraph</span>');
// insertBefore places the new element as a sibling
$('<span> Insert Before Paragraph</span>').insertBefore('#randPara');
// prependTo places the new element as the first child of the element
$('<span>Prepend To Paragraph</span>').prependTo('#randPara');
// appendTo places the new element as a child
$('<span>Append To Paragraph</span>').appendTo('#randPara');
// append places the new element as the last child of the element
$('#randPara').append('<span> Append Paragraph</span>');
<div>
<h3 id="h3Tag">H3 Tag</h3>
</div>
<br />
<div id="randPara">
<p id="firstP">The First Paragraph</p>
</div>
<br /><br />
<form>
<button type="button" id="deleteSpan">Delete Spans</button>
</form>
<br />
</body>
</html>
PART 6:
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<title>JQuery Animations</title>
<script type="text/javascript" src="http://code.jquery.com/jquery-latest.js"></script>
<script type="text/javascript"
src="https://ajax.googleapis.com/ajax/libs/jqueryui/1.8.10/jquery-ui.min.js"></script>
<script type="text/javascript">
$("document").ready(function() {
$('#hideLogo').bind('click', hideTheImage);
$('#showLogo').bind('click', showTheImage);
$('#toggleText').bind('click', toggleTheText);
$('#fadeLogo').bind('click', fadeTheImage);
$('#fadeALittle').bind('click', fadeALittle);
$('#fullOpacity').bind('click', fullOpacityImage);
$('#slideAway').bind('click', slideTheImage);
$('#slideBack').bind('click', unslideTheImage);
$('#customAnim').bind('click', customAnimate);
$('#shakeLogo').bind('click', shakeLogo);
$('#bounceLogo').bind('click', bounceLogo);
$('#highlightLogo').bind('click', highlightLogo);
$('#pulsateLogo').bind('click', pulsateLogo);
$('#sizeLogo').bind('click', sizeLogo);
});
function hideTheImage() {
$('#logo').hide('puff',{}, 2500);
// I could also hide with effects .hide('effect', {properties}, 1000);
// Effects: blind, clip, drop, explode, fade, fold, puff, slide, scale
}
function showTheImage() {
$('#logo').show('fold', {}, 2500);
}
// Displays or hides matching elements on the page
function toggleTheText()
{
$("h4").toggle(2500);
}
function fadeTheImage() {
$('#logo').fadeOut(2500);
}
function fadeALittle() {
$('#logo').fadeTo(2500,.30);
}
function fullOpacityImage() {
$('#logo').fadeTo(2500,1);
}
function slideTheImage() {
$('#second').slideUp(2500);
}
function unslideTheImage() {
$('#second').slideDown(2500);
}
// animate only allows linear or swing easing
// With the JQuery UI Library you get many more easeOutElastic, easeInCirc,
// easeInOutExpo, easeOutBack, easeInOutElastic, easeOutBounce
function customAnimate() {
<body>
<div><h3>Playing with Animation's</h3></div>
<div id="logoDiv">
<img src='http://www.newthinktank.com/images/ThinkTankLogo.gif' id="logo" height='75'
alt='Little Brain' />
</div>
<br />
<div>
<h4>Message 1</h4>
<h4 style="display: none">Message 2</h4>
</div>
<div>
<p id="second">Here today, gone in a click!</p>
</div>
<br />
<form>
<button type="button" id="hideLogo">Hide the Logo</button>
<button type="button" id="showLogo">Show the Logo</button>
<button type="button" id="fadeLogo">Fade the Logo</button>
<button type="button" id="fadeALittle">Fade a Little</button>
<button type="button" id="fullOpacity">Full Opacity</button>
<button type="button" id="toggleText">Toggle the Text</button>
<button type="button" id="slideAway">Slide away Text</button>
<button type="button" id="slideBack">Slide Text Back</button>
<button type="button" id="customAnim">Custom Animation</button>
<button type="button" id="shakeLogo">Shake Logo</button>
<button type="button" id="bounceLogo">Bounce Logo</button>
<button type="button" id="highlightLogo">Highlight Logo</button>
<button type="button" id="pulsateLogo">Pulsate Logo</button>
<button type="button" id="sizeLogo">Size Logo</button>
</form>
</body>
</html>
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<title> Accordian with code </title>
<script type="text/javascript" src="http://code.jquery.com/jquery-latest.js"></script>
<script type="text/javascript"
src="https://ajax.googleapis.com/ajax/libs/jqueryui/1.8.10/jquery-ui.min.js"> </script>
<style>
h4 {
background-color:DarkSlateGray;
color:white;
padding:10px;
}
div {
background-color:LightCyan;
width:200px;
overflow:hidden;
}
</style>
<script type="text/javascript">
$("document").ready(function() {
$('#superHumans > div').hide();
$('#superHumans h4').click(function() {
$(this).siblings('div:visible').slideUp('2000');
$(this).next().animate({'height':'toggle'}, '2000', 'easeInOutExpo'
);
});
});
</script>
</head>
<body>
<div id="superHumans">
<h4>Beyonder</h4>
<div>
He had the power to achieve what ever he pleased. He has vast power to alter reality on a
planetary scale.
</div>
<h4>Thanos</h4>
<div>
Gifted with immortality and unaffected by attacks of any kind.
</div>
<h4>Galactus</h4>
<div>
He can levitate; restructure molecules; convert matter into energy and vice-versa; project
energy with enormous concussive force; teleport himself, others, and objects across space,
dimensions, and time; erect virtually impenetrable fields; restore others from any injury, even
total disintegration; and endow or enhance powers within others.
</div>
<h4>Franklin Richards</h4>
<div>
He has vast telepathic powers, tremendous telekinetic powers, the ability to fire enormously
powerful energy blasts, and the ability to rearrange the molecular structure of matter, even
up to a cosmic scale.
</div>
<h4>Doctor Manhattan</h4>
<div>
Mastery over the manipulation of all matter and energy. Knowledge of all that has occurred in
the past and present.
</div>
</div>
</body>
</html>