JQuery Tutorial
JQuery Tutorial
www.ebooktutorial.blogspot.in
Content downloaded from www.w3schools.com. All Reserved to www.w3schools.com
Content
jQuery Tutorial
jQuery HTML
jQuery HOME
jQuery Get
jQuery Intro
jQuery Set
jQuery Add
jQuery Syntax
jQuery Remove
jQuery Selectors
jQuery Events
jQuery css()
jQuery Dimensions
jQuery Effects
jQuery Hide/Show
jQuery Fade
jQuery Slide
jQuery Animate
jQuery stop()
jQuery Callback
jQuery Chaining
jQuery Traversing
jQuery Traversing
jQuery Ancestors
jQuery Descendants
jQuery Siblings
jQuery Filtering
Created by www.ebooktutorials.blogspot.in
jQuery Tutorial
jQuery greatly simplifies JavaScript programming.
jQuery is easy to learn.
jQuery is a JavaScript Library.
Example
$(document).ready(function(){
$("p").click(function(){
$(this).hide();
});
});
Created by www.ebooktutorials.blogspot.in
jQuery Introduction
The purpose of jQuery is to make it much easier to use JavaScript on your
website.
HTML
CSS
JavaScript
If you want to study these subjects first, find the tutorials on our Home page.
What is jQuery?
jQuery is a lightweight, "write less, do more", JavaScript library.
The purpose of jQuery is to make it much easier to use JavaScript on your
website.
jQuery takes a lot of common tasks that require many lines of JavaScript code
to accomplish, and wraps them into methods that you can call with a single
line of code.
jQuery also simplifies a lot of the complicated things from JavaScript, like
AJAX calls and DOM manipulation.
The jQuery library contains the following features:
HTML/DOM manipulation
CSS manipulation
HTML event methods
Effects and animations
AJAX
Utilities
Tip: In addition, jQuery has plugins for almost any task out there.
Why jQuery?
There are lots of other JavaScript frameworks out there, but jQuery seems to
be the most popular, and also the most extendable.
Many of the biggest companies on the Web use jQuery, such as:
Google
Microsoft
IBM
Netflix
Created by www.ebooktutorials.blogspot.in
Downloading jQuery
There are two versions of jQuery available for downloading:
Production version - this is for your live website because it has been
minified and compressed
Development version - this is for testing and development (uncompressed
and readable code)
Created by www.ebooktutorials.blogspot.in
Microsoft CDN:
<head>
<script src="http://ajax.aspnetcdn.com/ajax/jQuery/jquery1.11.2.min.js"></script>
</head>
One big advantage of using the hosted jQuery from Google or Microsoft:
Many users already have downloaded jQuery from Google or Microsoft when
visiting another site. As a result, it will be loaded from cache when they visit
your site, which leads to faster loading time. Also, most CDN's will make sure
that once a user requests a file from it, it will be served from the server closest
to them, which also leads to faster loading time.
Created by www.ebooktutorials.blogspot.in
jQuery Syntax
With jQuery you select (query) HTML elements and perform "actions" on them.
jQuery Syntax
The jQuery syntax is tailor made for selecting HTML elements and performing
some action on the element(s).
Basic syntax is: $(selector).action()
Examples:
$(this).hide() - hides the current element.
$("p").hide() - hides all <p> elements.
$(".test").hide() - hides all elements with class="test".
$("#test").hide() - hides the element with id="test".
Are you familiar with CSS selectors?
jQuery uses CSS syntax to select elements. You will learn more about the
selector syntax in the next chapter of this tutorial.
The Document Ready Event
You might have noticed that all jQuery methods in our examples, are inside a
document ready event:
$(document).ready(function(){
// jQuery methods go here...
});
This is to prevent any jQuery code from running before the document is
finished loading (is ready).
It is good practice to wait for the document to be fully loaded and ready before
working with it. This also allows you to have your JavaScript code before the
body of your document, in the head section.
Here are some examples of actions that can fail if methods are run before the
document is fully loaded:
Tip: The jQuery team has also created an even shorter method for the
document ready event:
$(function(){
// jQuery methods go here...
});
Created by www.ebooktutorials.blogspot.in
jQuery Selectors
jQuery selectors are one of the most important parts of the jQuery library.
jQuery Selectors
jQuery selectors allow you to select and manipulate HTML element(s).
jQuery selectors are used to "find" (or select) HTML elements based on their id,
classes, types, attributes, values of attributes and much more. It's based on the
existing CSS Selectors, and in addition, it has some own custom selectors.
All selectors in jQuery start with the dollar sign and parentheses: $().
Example
When a user clicks on a button, all <p> elements will be hidden:
Example
$(document).ready(function(){
$("button").click(function(){
$("p").hide();
});
});
Example
When a user clicks on a button, the element with id="test" will be hidden:
Example
$(document).ready(function(){
$("button").click(function(){
$("#test").hide();
});
});
Created by www.ebooktutorials.blogspot.in
Example
When a user clicks on a button, the elements with class="test" will be hidden:
Example
$(document).ready(function(){
$("button").click(function(){
$(".test").hide();
});
});
Description
Example
$("*")
Try it
$(this)
$("p.intro")
$("p:first")
$("ul li:first")
$("ul li:first-child")
$("[href]")
$("a[target='_blank']")
Try
Try
Try
Try
Try
Try
Try
it
it
it
it
it
it
it
Try it
Try it
Try it
Try it
Example
<head>
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.11.2/jquery.min.js">
</script>
<script src="my_jquery_functions.js"></script>
</head>
Created by www.ebooktutorials.blogspot.in
The term "fires" is often used with events. Example: "The keypress event fires
the moment you press a key".
Here are some common DOM events:
Mouse Events Keyboard Events Form Events Document/Window Events
click
keypress
submit
load
dblclick
keydown
change
resize
mouseenter
keyup
focus
scroll
blur
unload
mouseleave
jQuery Syntax For Event Methods
$(document).ready()
click()
Example
$("p").click(function(){
$(this).hide();
});
dblclick()
mouseenter()
mouseleave()
mousedown()
mouseup()
Created by www.ebooktutorials.blogspot.in
Example
$("#p1").mouseup(function(){
alert("Mouse up over p1!");
});
hover()
focus()
The focus() method attaches an event handler function to an HTML form field.
The function is executed when the form field gets focus:
Example
$("input").focus(function(){
$(this).css("background-color", "#cccccc");
});
blur()
The blur() method attaches an event handler function to an HTML form field.
The function is executed when the form field loses focus:
Example
$("input").blur(function(){
$(this).css("background-color", "#ffffff");
});
Created by www.ebooktutorials.blogspot.in
Example
$("#hide").click(function(){
$("p").hide();
});
$("#show").click(function(){
$("p").show();
});
Syntax:
$(selector).hide(speed,callback);
$(selector).show(speed,callback);
The optional speed parameter specifies the speed of the hiding/showing, and
can take the following values: "slow", "fast", or milliseconds.
The optional callback parameter is a function to be executed after the hide() or
show() method completes (you will learn more about callback functions in a
later chapter).
The following example demonstrates the speed parameter with hide():
Example
$("button").click(function(){
$("p").hide(1000);
});
jQuery toggle()
With jQuery, you can toggle between the hide() and show() methods with the
toggle() method.
Shown elements are hidden and hidden elements are shown:
Example
$("button").click(function(){
$("p").toggle();
});
Syntax:
$(selector).toggle(speed,callback);
The optional speed parameter can take the following values: "slow", "fast", or
milliseconds.
The optional callback parameter is a function to be executed after toggle()
completes.
Created by www.ebooktutorials.blogspot.in
Example:
<!DOCTYPE html>
<html>
<head>
<script
src="http://ajax.googleapis.com/ajax/libs/jquery/1.11.2/jquery.min.js"></s
cript>
<script>
$(document).ready(function(){
$("#hide").click(function(){
$("p").hide();
});
$("#show").click(function(){
$("p").show();
});
});
</script>
</head>
<body>
<button id="hide">Hide</button>
<button id="show">Show</button>
</body>
</html>
Created by www.ebooktutorials.blogspot.in
fadeIn()
fadeOut()
fadeToggle()
fadeTo()
The optional speed parameter specifies the duration of the effect. It can take
the following values: "slow", "fast", or milliseconds.
The optional callback parameter is a function to be executed after the fading
completes.
The following example demonstrates the fadeIn() method with different
parameters:
Example
$("button").click(function(){
$("#div1").fadeIn();
$("#div2").fadeIn("slow");
$("#div3").fadeIn(3000);
});
The optional speed parameter specifies the duration of the effect. It can take
the following values: "slow", "fast", or milliseconds.
The optional callback parameter is a function to be executed after the fading
completes.
The following example demonstrates the fadeOut() method with different
parameters:
Example
$("button").click(function(){
$("#div1").fadeOut();
$("#div2").fadeOut("slow");
$("#div3").fadeOut(3000);
});
Created by www.ebooktutorials.blogspot.in
The optional speed parameter specifies the duration of the effect. It can take
the following values: "slow", "fast", or milliseconds.
The optional callback parameter is a function to be executed after the fading
completes.
The following example demonstrates the fadeToggle() method with different
parameters:
Example
$("button").click(function(){
$("#div1").fadeToggle();
$("#div2").fadeToggle("slow");
$("#div3").fadeToggle(3000);
});
The required speed parameter specifies the duration of the effect. It can take
the following values: "slow", "fast", or milliseconds.
The required opacity parameter in the fadeTo() method specifies fading to a
given opacity (value between 0 and 1).
The optional callback parameter is a function to be executed after the function
completes.
The following example demonstrates the fadeTo() method with different
parameters:
Example
$("button").click(function(){
$("#div1").fadeTo("slow", 0.15);
$("#div2").fadeTo("slow", 0.4);
$("#div3").fadeTo("slow", 0.7);
});
Created by www.ebooktutorials.blogspot.in
Created by www.ebooktutorials.blogspot.in
slideDown()
slideUp()
slideToggle()
The optional speed parameter specifies the duration of the effect. It can take
the following values: "slow", "fast", or milliseconds.
The optional callback parameter is a function to be executed after the sliding
completes.
The following example demonstrates the slideDown() method:
Example
$("#flip").click(function(){
$("#panel").slideDown();
});
The optional speed parameter specifies the duration of the effect. It can take
the following values: "slow", "fast", or milliseconds.
The optional callback parameter is a function to be executed after the sliding
completes.
The following example demonstrates the slideUp() method:
Example
$("#flip").click(function(){
$("#panel").slideUp();
});
Created by www.ebooktutorials.blogspot.in
The optional speed parameter can take the following values: "slow", "fast",
milliseconds.
The optional callback parameter is a function to be executed after the sliding
completes.
The following example demonstrates the slideToggle() method:
Example
$("#flip").click(function(){
$("#panel").slideToggle();
});
Example:
<!DOCTYPE html>
<html>
<head>
<script
src="http://ajax.googleapis.com/ajax/libs/jquery/1.11.2/jquery.min.js"></scri
pt>
<script>
$(document).ready(function(){
$("#flip").click(function(){
$("#panel").slideDown("slow");
});
});
</script>
<style>
#panel, #flip {
padding: 5px;
text-align: center;
background-color: #e5eecc;
border: solid 1px #c3c3c3;
}
#panel {
padding: 50px;
display: none;
}
</style>
</head>
<body>
<div id="flip">Click to slide down panel</div>
<div id="panel">Hello world!</div>
</body>
</html>
Created by www.ebooktutorials.blogspot.in
Example
$("button").click(function(){
$("div").animate({left: '250px'});
});
By default, all HTML elements have a static position, and cannot be moved.
To manipulate the position, remember to first set the CSS position property of
the element to relative, fixed, or absolute!
Example
$("button").click(function(){
$("div").animate({
left: '250px',
opacity: '0.5',
height: '150px',
width: '150px'
});
});
Created by www.ebooktutorials.blogspot.in
Example
$("button").click(function(){
$("div").animate({
left: '250px',
height: '+=150px',
width: '+=150px'
});
});
Example
$("button").click(function(){
$("div").animate({
height: 'toggle'
});
});
Example 1
$("button").click(function(){
var div = $("div");
div.animate({height: '300px', opacity: '0.4'}, "slow");
div.animate({width: '300px', opacity: '0.8'}, "slow");
div.animate({height: '100px', opacity: '0.4'}, "slow");
div.animate({width: '100px', opacity: '0.8'}, "slow");
});
The example below first moves the <div> element to the right, and then
increases the font size of the text:
Example 2
$("button").click(function(){
var div = $("div");
div.animate({left: '100px'}, "slow");
div.animate({fontSize: '3em'}, "slow");
});
Created by www.ebooktutorials.blogspot.in
Example:
<!DOCTYPE html>
<html>
<head>
<script
src="http://ajax.googleapis.com/ajax/libs/jquery/1.11.2/jquery.min.js"></scri
pt>
<script>
$(document).ready(function(){
$("button").click(function(){
var div = $("div");
div.animate({left: '100px'}, "slow");
div.animate({fontSize: '3em'}, "slow");
});
});
</script>
</head>
<body>
<button>Start Animation</button>
<p>By default, all HTML elements have a static position, and cannot be moved.
To manipulate the position, remember to first set the CSS position property
of the element to relative, fixed, or absolute!</p>
<div
style="background:#98bf21;height:100px;width:200px;position:absolute;">HELLO<
/div>
</body>
</html>
Created by www.ebooktutorials.blogspot.in
The optional stopAll parameter specifies whether also the animation queue
should be cleared or not. Default is false, which means that only the active
animation will be stopped, allowing any queued animations to be performed
afterwards.
The optional goToEnd parameter specifies whether or not to complete the
current animation immediately. Default is false.
So, by default, the stop() method kills the current animation being performed
on the selected element.
The following example demonstrates the stop() method, with no parameters:
Example
$("#stop").click(function(){
$("#panel").stop();
});
Created by www.ebooktutorials.blogspot.in
Example
<!DOCTYPE html>
<html>
<head>
<script
src="http://ajax.googleapis.com/ajax/libs/jquery/1.11.2/jquery.min.js"></scri
pt>
<script>
$(document).ready(function(){
$("#flip").click(function(){
$("#panel").slideDown(5000);
});
$("#stop").click(function(){
$("#panel").stop();
});
});
</script>
<style>
#panel, #flip {
padding: 5px;
font-size: 18px;
text-align: center;
background-color: #555;
color: white;
border: solid 1px #666;
border-radius: 3px;
}
#panel {
padding: 50px;
display: none;
}
</style>
</head>
<body>
<button id="stop">Stop sliding</button>
<div id="flip">Click to slide down panel</div>
<div id="panel">Hello world!</div>
</body>
</html>
Created by www.ebooktutorials.blogspot.in
Created by www.ebooktutorials.blogspot.in
jQuery Chaining
With jQuery, you can chain together actions/methods.
Chaining allows us to run multiple jQuery methods (on the same element)
within a single statement.
Example
$("#p1").css("color", "red").slideUp(2000).slideDown(2000);
Example
$("#p1").css("color", "red")
.slideUp(2000)
.slideDown(2000);
jQuery throws away extra whitespace and executes the lines above as one long
line of code.
Created by www.ebooktutorials.blogspot.in
Example
<!DOCTYPE html>
<html>
<head>
<script
src="http://ajax.googleapis.com/ajax/libs/jquery/1.11.2/jquery.min.js"
></script>
<script>
$(document).ready(function(){
$("button").click(function(){
$("#p1").css("color", "red")
.slideUp(2000)
.slideDown(2000);
});
});
</script>
</head>
<body>
<p id="p1">jQuery is fun!!</p>
<button>Click me</button>
</body>
</html>
Created by www.ebooktutorials.blogspot.in
The following example demonstrates how to get content with the jQuery text()
and html() methods:
Example
$("#btn1").click(function(){
alert("Text: " + $("#test").text());
});
$("#btn2").click(function(){
alert("HTML: " + $("#test").html());
});
The following example demonstrates how to get the value of an input field with
the jQuery val() method:
Example
$("#btn1").click(function(){
alert("Value: " + $("#test").val());
});
Created by www.ebooktutorials.blogspot.in
Example
$("button").click(function(){
alert($("#w3s").attr("href"));
});
Example
<!DOCTYPE html>
<html>
<head>
<script
src="http://ajax.googleapis.com/ajax/libs/jquery/1.11.2/jquery.min.js"
></script>
<script>
$(document).ready(function(){
$("button").click(function(){
alert("Value: " + $("#test").val());
});
});
</script>
</head>
<body>
<p>Name: <input type="text" id="test" value="Mickey Mouse"></p>
<button>Show Value</button>
</body>
</html>
Created by www.ebooktutorials.blogspot.in
The following example demonstrates how to set content with the jQuery text(),
html(), and val() methods:
Example
$("#btn1").click(function(){
$("#test1").text("Hello world!");
});
$("#btn2").click(function(){
$("#test2").html("<b>Hello world!</b>");
});
$("#btn3").click(function(){
$("#test3").val("Dolly Duck");
});
$("#btn1").click(function(){
$("#test1").text(function(i, origText){
return "Old text: " + origText + " New text: Hello world!
(index: " + i + ")";
});
});
$("#btn2").click(function(){
$("#test2").html(function(i, origText){
return "Old html: " + origText + " New html: Hello <b>world!</b>
(index: " + i + ")";
});
});
Created by www.ebooktutorials.blogspot.in
Example
$("button").click(function(){
$("#w3s").attr("href", "http://www.w3schools.com/jquery");
});
The attr() method also allows you to set multiple attributes at the same time.
The following example demonstrates how to set both the href and title
attributes at the same time:
Example
$("button").click(function(){
$("#w3s").attr({
"href" : "http://www.w3schools.com/jquery",
"title" : "W3Schools jQuery Tutorial"
});
});
Example
<!DOCTYPE html>
<html>
<head><script
src="http://ajax.googleapis.com/ajax/libs/jquery/1.11.2/jquery.min.js"></s
cript>
<script>
$(document).ready(function(){
$("#btn1").click(function(){
$("#test1").text("Hello world!");
});
$("#btn2").click(function(){
$("#test2").html("<b>Hello world!</b>");
});
$("#btn3").click(function(){
$("#test3").val("Dolly Duck");
});
});
</script></head>
<body>
<p id="test1">This is a paragraph.</p>
<p id="test2">This is another paragraph.</p>
<p>Input field: <input type="text" id="test3" value="Mickey Mouse"></p>
<button id="btn1">Set Text</button>
<button id="btn2">Set HTML</button>
<button id="btn3">Set Value</button>
</body></html>
Created by www.ebooktutorials.blogspot.in
Example
$("p").prepend("Some prepended text.");
Created by www.ebooktutorials.blogspot.in
Example
$("img").after("Some text after");
$("img").before("Some text before");
Example
function afterText() {
var txt1 = "<b>I </b>";
HTML
var txt2 = $("<i></i>").text("love ");
var txt3 = document.createElement("b");
txt3.innerHTML = "jQuery!";
$("img").after(txt1, txt2, txt3);
after img
}
Example
<!DOCTYPE html>
<html>
<head><script
src="http://ajax.googleapis.com/ajax/libs/jquery/1.11.2/jquery.min.js"></scri
pt>
<script>
$(document).ready(function(){
$("#btn1").click(function(){
$("p").prepend("<b>Prepended text</b>. ");
});
$("#btn2").click(function(){
$("ol").prepend("<li>Prepended item</li>");
});
});
</script></head>
<body>
<p>This is a paragraph.</p>
<p>This is another paragraph.</p>
<ol>
<li>List item 1</li>
<li>List item 2</li>
<li>List item 3</li>
</ol>
<button id="btn1">Prepend text</button>
<button id="btn2">Prepend list item</button>
</body></html>
Created by www.ebooktutorials.blogspot.in
Example
<!DOCTYPE html>
<html>
<head>
<script
src="http://ajax.googleapis.com/ajax/libs/jquery/1.11.2/jquery.min.js"></s
cript>
<script>
function afterText() {
var txt1 = "<b>I </b>";
// Create element with HTML
var txt2 = $("<i></i>").text("love ");
// Create with jQuery
var txt3 = document.createElement("b");
// Create with DOM
txt3.innerHTML = "jQuery!";
$("img").after(txt1, txt2, txt3);
// Insert new elements after
img
}
</script>
</head>
<body>
<img src="/images/w3jquery.gif" alt="jQuery" width="100" height="140">
<p>Click the button to insert text after the image.</p>
<button onclick="afterText()">Insert after</button>
</body>
</html>
Example
<!DOCTYPE html>
<html>
<head>
<script
src="http://ajax.googleapis.com/ajax/libs/jquery/1.11.2/jquery.min.js"
></script>
<script>
$(document).ready(function(){
$("#btn1").click(function(){
$("img").before("<b>Before</b>");
});
$("#btn2").click(function(){
$("img").after("<i>After</i>");
});
});
</script>
</head>
<body>
<img src="/images/w3jquery.gif" alt="jQuery" width="100"
height="140"><br><br>
<button id="btn1">Insert before</button>
<button id="btn2">Insert after</button>
</body>
</html>
Created by www.ebooktutorials.blogspot.in
Remove Elements/Content
To remove elements and content, there are mainly two jQuery methods:
remove() - Removes the selected element (and its child elements)
empty() - Removes the child elements from the selected element
Example
<!DOCTYPE html>
<html><head>
<script
src="http://ajax.googleapis.com/ajax/libs/jquery/1.11.2/jquery.min.js"></scri
pt>
<script>
$(document).ready(function(){
$("button").click(function(){
$("#div1").remove();
});
});
</script></head>
<body>
<div id="div1" style="height:100px;width:300px;border:1px solid
black;background-color:yellow;">
This is some text in the div.
<p>This is a paragraph in the div.</p>
<p>This is another paragraph in the div.</p>
</div><br>
<button>Remove div element</button>
</body></html>
Created by www.ebooktutorials.blogspot.in
Example Stylesheet
The following stylesheet will be used for all the examples on this page:
.important {
font-weight: bold;
font-size: xx-large;
}
.blue {
color: blue;
}
Example
$("button").click(function(){
$("h1, h2, p").addClass("blue");
$("div").addClass("important");
});
You can also specify multiple classes within the addClass() method:
Example
$("button").click(function(){
$("#div1").addClass("important blue");
});
Example
$("button").click(function(){
$("h1, h2, p").removeClass("blue");
});
Created by www.ebooktutorials.blogspot.in
Example
$("button").click(function(){
$("h1, h2, p").toggleClass("blue");
});
Example
<!DOCTYPE html>
<html>
<head>
<script
src="http://ajax.googleapis.com/ajax/libs/jquery/1.11.2/jquery.min.js"
></script>
<script>
$(document).ready(function(){
$("button").click(function(){
$("h1, h2, p").toggleClass("blue");
});
});
</script>
<style>
.blue {
color: blue;
}
</style>
</head>
<body>
<h1>Heading 1</h1>
<h2>Heading 2</h2>
<p>This is a paragraph.</p>
<p>This is another paragraph.</p>
<button>Toggle class</button>
</body>
</html>
Created by www.ebooktutorials.blogspot.in
The following example will return the background-color value of the FIRST
matched element:
Example
$("p").css("background-color");
The following example will set the background-color value for ALL matched
elements:
Example
$("p").css("background-color", "yellow");
The following example will set a background-color and a font-size for ALL
matched elements:
Example
$("p").css({"background-color": "yellow", "font-size": "200%"});
Created by www.ebooktutorials.blogspot.in
Example
<!DOCTYPE html>
<html>
<head>
<script
src="http://ajax.googleapis.com/ajax/libs/jquery/1.11.2/jquery.min.js"
></script>
<script>
$(document).ready(function(){
$("button").click(function(){
$("p").css({"background-color": "yellow", "font-size":
"200%"});
});
});
</script>
</head>
<body>
<h2>This is a heading</h2>
<p style="background-color:#ff0000">This is a paragraph.</p>
<p style="background-color:#00ff00">This is a paragraph.</p>
<p style="background-color:#0000ff">This is a paragraph.</p>
<p>This is a paragraph.</p>
<button>Set multiple styles for p</button>
</body>
</html>
Created by www.ebooktutorials.blogspot.in
jQuery - Dimensions
With jQuery, it is easy to work with the dimensions of elements and browser
window.
width()
height()
innerWidth()
innerHeight()
outerWidth()
outerHeight()
jQuery Dimensions
Created by www.ebooktutorials.blogspot.in
Created by www.ebooktutorials.blogspot.in
Example
$("button").click(function(){
var txt = "";
txt += "Document width/height: " + $(document).width();
txt += "x" + $(document).height() + "\n";
txt += "Window width/height: " + $(window).width();
txt += "x" + $(window).height();
alert(txt);
});
The following example sets the width and height of a specified <div> element:
Example
$("button").click(function(){
$("#div1").width(500).height(500);
});
Example
<!DOCTYPE html>
<html>
<head>
<script
src="http://ajax.googleapis.com/ajax/libs/jquery/1.11.2/jquery.min.js"
></script>
<script>
$(document).ready(function(){
$("button").click(function(){
var txt = "";
txt += "Document width/height: " + $(document).width();
txt += "x" + $(document).height() + "\n";
txt += "Window width/height: " + $(window).width();
txt += "x" + $(window).height();
alert(txt);
});
});
</script>
</head>
<body>
<button>Display dimensions of document and window</button>
</body>
</html>
Created by www.ebooktutorials.blogspot.in
jQuery Traversing
What is Traversing?
jQuery traversing, which means "move through", are used to "find" (or select)
HTML elements based on their relation to other elements. Start with one
selection and move through that selection until you reach the elements you
desire.
The image below illustrates a family tree. With jQuery traversing, you can
easily move up (ancestors), down (descendants) and sideways (siblings) in the
family tree, starting from the selected (current) element. This movement is
called traversing - or moving through - the DOM.
Illustration explained:
Created by www.ebooktutorials.blogspot.in
You can also use an optional parameter to filter the search for ancestors.
The following example returns all ancestors of all <span> elements that are
<ul> elements:
Example
$(document).ready(function(){
$("span").parents("ul");
});
Created by www.ebooktutorials.blogspot.in
Example
<!DOCTYPE html>
<html>
<head>
<style>
.ancestors * {
display: block;
border: 2px solid lightgrey;
color: lightgrey;
padding: 5px;
margin: 15px;
}
</style>
<script
src="http://ajax.googleapis.com/ajax/libs/jquery/1.11.2/jquery.min.js"
></script>
<script>
$(document).ready(function(){
$("span").parents().css({"color": "red", "border": "2px solid
red"});
});
</script>
</head>
<body class="ancestors">body (great-great-grandparent)
<div style="width:500px;">div (great-grandparent)
<ul>ul (grandparent)
<li>li (direct parent)
<span>span</span>
</li>
</ul>
</div>
</body>
<!-- The outer red border, before the body element, is the html
element (also an ancestor) -->
</html>
Created by www.ebooktutorials.blogspot.in
children()
find()
You can also use an optional parameter to filter the search for children.
The following example returns all <p> elements with the class name "1", that
are direct children of <div>:
Example
$(document).ready(function(){
$("div").children("p.1");
});
Created by www.ebooktutorials.blogspot.in
Example
<!DOCTYPE html>
<html>
<head>
<style>
.descendants * {
display: block;
border: 2px solid lightgrey;
color: lightgrey;
padding: 5px;
margin: 15px;
}
</style>
<script
src="http://ajax.googleapis.com/ajax/libs/jquery/1.11.2/jquery.min.js"
></script>
<script>
$(document).ready(function(){
$("div").find("span").css({"color": "red", "border": "2px solid
red"});
});
</script>
</head>
<body>
<div class="descendants" style="width:500px;">div (current element)
<p>p (child)
<span>span (grandchild)</span>
</p>
<p>p (child)
<span>span (grandchild)</span>
</p>
</div>
</body>
</html>
Created by www.ebooktutorials.blogspot.in
siblings()
next()
nextAll()
nextUntil()
prev()
prevAll()
prevUntil()
Example
$(document).ready(function(){
$("h2").siblings();
});
You can also use an optional parameter to filter the search for siblings.
The following example returns all sibling elements of <h2> that are <p> elements:
Example
$(document).ready(function(){
$("h2").siblings("p");
});
Example
$(document).ready(function(){
$("h2").next();
});
Example
$(document).ready(function(){
$("h2").nextAll();
});
Created by www.ebooktutorials.blogspot.in
Example
$(document).ready(function(){
$("h2").nextUntil("h6");
});
Example 1
<!DOCTYPE html>
<html>
<head>
<script
src="http://ajax.googleapis.com/ajax/libs/jquery/1.11.2/jquery.min.js"></scri
pt>
<script>
$(document).ready(function(){
$("p").filter(".intro").css("background-color", "yellow");
});
</script>
</head>
<body>
<h1>Welcome to My Homepage</h1>
<p>My name is Donald.</p>
<p class="intro">I live in Duckburg.</p>
<p class="intro">I love Duckburg.</p>
<p>My best friend is Mickey.</p>
</body>
</html>
Example 2
<!DOCTYPE html>
<html>
<head>
<script
src="http://ajax.googleapis.com/ajax/libs/jquery/1.11.2/jquery.min.js"
></script>
<script>
$(document).ready(function(){
$("p").eq(1).css("background-color", "yellow");
});
</script>
</head>
<body>
<h1>Welcome to My Homepage</h1>
<p>My name is Donald (index 0).</p>
<p>Donald Duck (index 1).</p>
<p>I live in Duckburg (index 2).</p>
<p>My best friend is Mickey (index 3).</p>
</body>
</html>
Created by www.ebooktutorials.blogspot.in
Example
$(document).ready(function(){
$("div p").first();
});
Example
$(document).ready(function(){
$("div p").last();
});
Example
$(document).ready(function(){
$("p").eq(1);
});
Example
$(document).ready(function(){
$("p").filter(".intro");
});
Created by www.ebooktutorials.blogspot.in
Example
$(document).ready(function(){
$("p").not(".intro");
});
Example
<!DOCTYPE html>
<html>
<head>
<script
src="http://ajax.googleapis.com/ajax/libs/jquery/1.11.2/jquery.min.js"></s
cript>
<script>
$(document).ready(function(){
$("div p").first().css("background-color", "yellow");
$("div p").last().css("background-color", "yellow");
});
</script>
</head>
<body>
<h1>Welcome to My Homepage</h1>
<p>This is the first paragraph in body.</p>
<div style="border: 1px solid black;">
<p>This is the first paragraph in a div.</p>
<p>This is the last paragraph in a div.</p>
</div><br>
<div style="border: 1px solid black;">
<p>This is the first paragraph in another div.</p>
<p>This is the last paragraph in another div.</p>
</div>
<p>This is the last paragraph in body.</p>
</body>
</html>
Created by www.ebooktutorials.blogspot.in