0% found this document useful (0 votes)
1 views56 pages

Unit 4 - Full Stack Development

jQuery is a popular JavaScript library designed to simplify HTML document manipulation, event handling, and animation. It allows developers to perform complex tasks with less code by providing easy-to-use methods for common operations. The document also covers how to include jQuery in web pages, its syntax, selectors, event methods, and DOM manipulation techniques.
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
1 views56 pages

Unit 4 - Full Stack Development

jQuery is a popular JavaScript library designed to simplify HTML document manipulation, event handling, and animation. It allows developers to perform complex tasks with less code by providing easy-to-use methods for common operations. The document also covers how to include jQuery in web pages, its syntax, selectors, event methods, and DOM manipulation techniques.
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

1

Page

FSD-Unit III Dr. Y. Jeevan Nagendra Kumar


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.

There are lots of other JavaScript libraries out there, but jQuery is probably the
most popular, and also the most extendable.

Many of the biggest companies on the Web use jQuery, such as:

• Google
• Microsoft
• IBM
• Netflix

jQuery Features

o HTML manipulation
o DOM manipulation

o DOM element selection


o CSS manipulation

o Effects and Animations


o Utilities
2
Page

FSD-Unit III Dr. Y. Jeevan Nagendra Kumar


Adding jQuery to Your Web Pages

There are several ways to start using jQuery on your web site. You can:

• Download the jQuery library from [Link]


• Include jQuery from a CDN, like Google

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)

Both versions can be downloaded from [Link].

• The jQuery library is a single JavaScript file, and you reference it with the
HTML <script> tag (notice that the <script> tag should be inside
the <head> section):
• <head>
<script src="[Link]"></script>
</head>

jQuery CDN
If you don't want to download and host jQuery yourself, you can include it from a CDN
(Content Delivery Network).
Google is an example of someone who host jQuery:
<head>
<script src="[Link]
</script>
3

</head>
Page

FSD-Unit III Dr. Y. Jeevan Nagendra Kumar


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()

• A $ sign to define/access jQuery


• A (selector) to "query (or find)" HTML elements
• A jQuery action() to be performed on the element(s)
• $(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".
jQuery uses CSS syntax to select elements.

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.
The jQuery team has also created an even shorter method for the document ready
event:
$(function(){

// jQuery methods go here...


4

});
Page

FSD-Unit III Dr. Y. Jeevan Nagendra Kumar


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 name, 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: $().

1. The element Selector

The jQuery element selector selects elements based on the element name.

You can select all <p> elements on a page like this:

$("p")

Example: When a user clicks on a button, all <p> elements will be hidden:

<!DOCTYPE html>
<html>
<head>
<script src="[Link]"></script>
<script>
$(document).ready(function(){
$("button").click(function(){
$("p").hide();
});
});
</script>
</head>
<body>
5

<h2>This is a heading</h2>
Page

FSD-Unit III Dr. Y. Jeevan Nagendra Kumar


<p>This is a paragraph.</p>
<p>This is another paragraph.</p>

<button>Click me to hide paragraphs</button>

</body>
</html>

2. The #id Selector

The jQuery #id selector uses the id attribute of an HTML tag to find the specific element.

An id should be unique within a page, so you should use the #id selector when you want
to find a single, unique element.

To find an element with a specific id, write a hash character, followed by the id of the
HTML element:

$("#test")

Example

When a user clicks on a button, the element with id="test" will be hidden:

<!DOCTYPE html>
<html>
<head>
<script src="[Link]"></script>
<script>
$(document).ready(function(){
$("button").click(function(){
$("#test").hide();
});
6
Page

});

FSD-Unit III Dr. Y. Jeevan Nagendra Kumar


</script>
</head>
<body>

<h2>This is a heading</h2>

<p>This is a paragraph.</p>
<p id="test">This is another paragraph.</p>

<button>Click me</button>

</body>
</html>

3. The .class Selector

The jQuery .class selector finds elements with a specific class.

To find elements with a specific class, write a period character, followed by the name of
the class:

$(".test")

Example

When a user clicks on a button, the elements with class="test" will be hidden:

<!DOCTYPE html>
<html>
<head>
<script src="[Link]"></script>
<script>
$(document).ready(function(){
$("button").click(function(){
7
Page

$(".test").hide();

FSD-Unit III Dr. Y. Jeevan Nagendra Kumar


});
});
</script>
</head>
<body>

<h2 class="test">This is a heading</h2>

<p class="test">This is a paragraph.</p>


<p>This is another paragraph.</p>

<button>Click me</button>

</body>
</html>

8
Page

FSD-Unit III Dr. Y. Jeevan Nagendra Kumar


Mouse Events

jQuery Syntax For Event Methods


In jQuery, most DOM events have an equivalent jQuery method.

To assign a click event to all paragraphs on a page, you can do this:

$("p").click();

The next step is to define what should happen when the event fires. You must pass a
function to the event:

$("p").click(function(){
// action goes here!!
});

1. click()

The click() method attaches an event handler function to an HTML element.

The function is executed when the user clicks on the HTML element.

The following example says: When a click event fires on a <p> element; hide the
current <p> element:
9
Page

FSD-Unit III Dr. Y. Jeevan Nagendra Kumar


<!DOCTYPE html>
<html>
<head>
<script src="[Link]"></script>
<script>
$(document).ready(function(){
$("p").click(function(){
$(this).hide();
});
});
</script>
</head>
<body>

<p>If you click on me, I will disappear.</p>


<p>Click me away!</p>
<p>Click me too!</p>

</body>
</html>

2. dblclick()

The dblclick() method attaches an event handler function to an HTML element.

The function is executed when the user double-clicks on the HTML element:

<!DOCTYPE html>
<html>
<head>
<script src="[Link]"></script>
<script>
10

$(document).ready(function(){
Page

FSD-Unit III Dr. Y. Jeevan Nagendra Kumar


$("p").dblclick(function(){
$(this).hide();
});
});
</script>
</head>
<body>

<p>If you double-click on me, I will disappear.</p>


<p>Click me away!</p>
<p>Click me too!</p>

</body>
</html>

3. mouseenter()

The mouseenter() method attaches an event handler function to an HTML element.

The function is executed when the mouse pointer enters the HTML element:

<!DOCTYPE html>
<html>
<head>
<script src="[Link]"></script>
<script>
$(document).ready(function(){
$("#p1").mouseenter(function(){
alert("You entered p1!");
});
});
</script>
11

</head>
Page

FSD-Unit III Dr. Y. Jeevan Nagendra Kumar


<body>

<p id="p1">Enter this paragraph.</p>

</body>
</html>

4. mouseleave()

The mouseleave() method attaches an event handler function to an HTML element.

The function is executed when the mouse pointer leaves the HTML element:

<!DOCTYPE html>
<html>
<head>
<script src="[Link]"></script>
<script>
$(document).ready(function(){
$("#p1").mouseleave(function(){
alert("Bye! You now leave p1!");
});
});
</script>
</head>
<body>

<p id="p1">This is a paragraph.</p>

</body>
</html>
12
Page

FSD-Unit III Dr. Y. Jeevan Nagendra Kumar


5. mousedown()

The mousedown() method attaches an event handler function to an HTML element.

The function is executed, when the left, middle or right mouse button is pressed down,
while the mouse is over the HTML element:

<!DOCTYPE html>
<html>
<head>
<script src="[Link]"></script>
<script>
$(document).ready(function(){
$("#p1").mousedown(function(){
alert("Mouse down over p1!");
});
});
</script>
</head>
<body>

<p id="p1">This is a paragraph.</p>

</body>
</html>

6. mouseup()

The mouseup() method attaches an event handler function to an HTML element.

The function is executed, when the left, middle or right mouse button is released, while
the mouse is over the HTML element:
13
Page

FSD-Unit III Dr. Y. Jeevan Nagendra Kumar


<!DOCTYPE html>
<html>
<head>
<script src="[Link]"></script>
<script>
$(document).ready(function(){
$("#p1").mouseup(function(){
alert("Mouse up over p1!");
});
});
</script>
</head>
<body>

<p id="p1">This is a paragraph.</p>

</body>
</html>

7. hover()

The hover() method takes two functions and is a combination of


the mouseenter() and mouseleave() methods.

The first function is executed when the mouse enters the HTML element, and the second
function is executed when the mouse leaves the HTML element:

<!DOCTYPE html>
<html>
<head>
<script src="[Link]"></script>
14
Page

FSD-Unit III Dr. Y. Jeevan Nagendra Kumar


<script>
$(document).ready(function(){
$("#p1").hover(function(){
alert("You entered p1!");
},
function(){
alert("Bye! You now leave p1!");
});
});
</script>
</head>
<body>

<p id="p1">This is a paragraph.</p>

</body>
</html>

15
Page

FSD-Unit III Dr. Y. Jeevan Nagendra Kumar


Form Events

1. submit()

jQuery submit event is sent to the element when the user attempts to submit a form.

The jQuery submit() method attach an event handler function to

the <form> elements that is executed when the user is attempting to submit a

form. The following example will display a message depending on the value
entered when you try to submit the form.

<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>Executing a Function on Form Submit Event in jQuery</title>
<script src="[Link]"></script>
<script>
$(document).ready(function(){
$("form").submit(function(){
alert("Form is submitted successfully.");
});
});
</script>
</head>
<body>

<h2>Form Element - Submit</h2>


<form action="">
Your Name: <input type="text" name="name" value="Nagendra"><br>
Your Experience: <input type="text" name="experience" value="18"><br><br>
<input type="submit" value="Submit">
</form>
</body>
</html>

2. Change:
The jQuery change() method attach an event handler function to

the <input>, <textarea> and <select> elements that is executed when its

value changes. The following example will display an alert message when you
16

select any option in dropdown select box.


Page

<!DOCTYPE html>

FSD-Unit III Dr. Y. Jeevan Nagendra Kumar


<html lang="en">
<head>
<meta charset="utf-8">
<title>Executing a Function on Change Event in jQuery</title>
<script src="[Link]"></script>
<script>
$(document).ready(function(){
$("select").change(function(){
var selectedOption = $(this).find(":selected").val();
alert("You have selected - " + selectedOption);
});
});
</script>
</head>
<body>
<form>
<label>City:</label>
<select>
<option>London</option>
<option>Paris</option>
<option>New York</option>
</select>
</form>
<p><strong>Note:</strong> Select any value from the dropdown select and see
the result.</p>
</body>
</html>

3. focus()

The focus() method attaches an event handler function to an HTML form field.

The function is executed when the form field gets focus:

4 . blur()

The blur() method attaches an event handler function to an HTML form field.

The function is executed when the form field loses focus:

<!DOCTYPE html>
<html>
<head>
<script src="[Link]"></script>
<script>
17

$(document).ready(function(){
$("input").focus(function(){
Page

FSD-Unit III Dr. Y. Jeevan Nagendra Kumar


$(this).css("background-color", "yellow");
});
$("input").blur(function(){
$(this).css("background-color", "lightblue");
});
});
</script>
</head>
<body>

Name: <input type="text" name="fullname"><br>


Email: <input type="text" name="email">

</body>
</html>

18
Page

FSD-Unit III Dr. Y. Jeevan Nagendra Kumar


DOM Manipulation

jQuery provides various methods to add, edit or delete DOM element(s) in the HTML
page. The following table lists some important methods to add/remove new DOM
elements.

19
Page

FSD-Unit III Dr. Y. Jeevan Nagendra Kumar


The following figure shows how the DOM manipulation methods add new elements.

jQuery after() Method

The jQuery after() method inserts content (new or existing DOM elements) after target
element(s) which is specified by a selector.

Syntax:
$('selector expression').after('content');

First of all, specify a selector to get the reference of target element(s) after which you
want to add the content and then call after() method. Pass the content string as a
parameter.
<!DOCTYPE html>
<html>
<head>
<script src="[Link]
</script>
<script>
$(document).ready(function () {

$('#div1').after('<div style="background-color:yellow"> New div </div>');


20

});
Page

</script>

FSD-Unit III Dr. Y. Jeevan Nagendra Kumar


<style>
div{
border: 1px solid;
background-color:red;
margin: 2px 0 2px 0;
}
</style>
</head>
<body>
<h1>Demo: jQuery after() method </h1>

<div id="div1">div 1
</div>

<div id="div2">div 2
</div>
</body>
</html>

jQuery before() Method


The jQuery before() method inserts content (new or existing DOM elements) before
target element(s) which is specified by a selector.

Syntax:
21

$('selector expression').before('content');
Page

FSD-Unit III Dr. Y. Jeevan Nagendra Kumar


Example:

<!DOCTYPE html>
<html><head>
<script src="[Link]"></script>
</script>
<script>
$(document).ready(function () {
$('#div1').before('<div style="background-color:yellow"> New div </div>');
});
</script>
<style>
div{
border: 1px solid;
background-color:red;
margin: 2px 0 2px 0;
}
</style>
</head>
<body>
<h1>Demo: jQuery before() method </h1>
<div id="div1">div 1
</div>

<div id="div2">div 2
</div>
</body> </html>
22
Page

FSD-Unit III Dr. Y. Jeevan Nagendra Kumar


jQuery append() Method
The jQuery append() method inserts content to the end of target element(s) which is
specified by a selector.

Syntax:
$('selector expression').append('content');

Example:

<!DOCTYPE html>
<html>
<head>
<script src="[Link]"></script>
</script>
<script>
$(document).ready(function () {

$('p').append('World!');

});
</script>
</head>
<body>
<h1>Demo: jQuery append() method </h1>
<p>Hello </p>
</body>
</html>
23
Page

FSD-Unit III Dr. Y. Jeevan Nagendra Kumar


jQuery prepend() Method
The jQuery prepend() method inserts content at the beginning of an element(s)
specified by a selector.

Syntax:
$('selector expression').prepend('content');
Example:
<!DOCTYPE html>
<html>
<head>
<script src="[Link]"></script>
</script>
<script>
$(document).ready(function () {

$('div').prepend('<p>This is prepended paragraph</p>');

});
</script>
</head>
<body>
<h1>Demo: jQuery prepend() method </h1>
<div>
<label>This is div.</label>
</div>
</body>
</html>
24
Page

FSD-Unit III Dr. Y. Jeevan Nagendra Kumar


jQuery remove() Method
The jQuery remove() method removes element(s) as specified by a selector.

Syntax:
$('selector expression').remove();

<!DOCTYPE html>
<html>
<head>
<script src="[Link]"></script>
</script>
<script>
$(document).ready(function () {

$('label').remove();

});
</script>
</head>
<body>
<h1>Demo: jQuery remove() method </h1>
<div>This is div.
<label>This is label.</label>
</div>
</body>
</html>
25
Page

FSD-Unit III Dr. Y. Jeevan Nagendra Kumar


jQuery replaceAll() Method
The jQuery replaceAll() method replaces all target elements with specified element(s).

Syntax:
$('content string').replaceAll('selector expression');
Example:
<!DOCTYPE html>
<html>
<head>
<script src="[Link]"></script>
</script>
<script>
$(document).ready(function () {

$('<span>This is span</span>').replaceAll('p');

});
</script>
</head>
<body>
<h1>Demo: jQuery replaceAll() method </h1>
<div>
<p>This is paragraph.</p>
</div>

<p>This is another paragraph.</p>


</body>
</html>
26
Page

FSD-Unit III Dr. Y. Jeevan Nagendra Kumar


jQuery wrap() Method
The jQuery wrap() method wrap each target element with specified content element.

Syntax:
$('selector expression').wrap('content string');

Example:
<!DOCTYPE html>
<html>
<head>
<script src="[Link]"></script>

<script>
$(document).ready(function () {

$('span').wrap('<p></p>');

});
</script>
</head>
<body>
<h1>Demo: jQuery wrap() method </h1>
<div>
<span>This is span.</span>
</div>
<span>This is span.</span>
</body>
</html>
27
Page

FSD-Unit III Dr. Y. Jeevan Nagendra Kumar


Effects & Animation
jQuery enables us to add effects on a web page. jQuery effects can be categorized into
fading, sliding, hiding/showing and animation effects.

1. Display Methods

a) jQuery hide() and show()

With jQuery, you can hide and show HTML elements with
the hide() and show() methods:

<!DOCTYPE html>
<html> <head>
<script src="[Link]"></script>
<script>
$(document).ready(function(){
$("#hide").click(function(){
28

$("p").hide();
Page

});

FSD-Unit III Dr. Y. Jeevan Nagendra Kumar


$("#show").click(function(){
$("p").show();
});
});
</script>
</head>
<body>
<p>If you click on the "Hide" button, I will disappear.</p>
<button id="hide">Hide</button>
<button id="show">Show</button>
</body>
</html>

<!DOCTYPE html>
<html> <head>
<script src="[Link]"></script>
<script>
$(document).ready(function(){
$("button").click(function(){
$("p").hide(1000);
});
});
</script>
</head>
<body>
<button>Hide</button>
<p>This is a paragraph with little content.</p>
<p>This is another small paragraph.</p>
29
Page

</body> </html>

FSD-Unit III Dr. Y. Jeevan Nagendra Kumar


b) jQuery toggle()

You can also toggle between hiding and showing an element with the toggle() method.

Shown elements are hidden and hidden elements are shown:

<!DOCTYPE html>
<html>
<head>
<script src="[Link]"></script>
<script>
$(document).ready(function(){
$("button").click(function(){
$("p").toggle();
});
});
</script>
</head>
<body>

<button>Toggle between hiding and showing the paragraphs</button>

<p>This is a paragraph with little content.</p>


<p>This is another small paragraph.</p>

</body>
</html>
30
Page

FSD-Unit III Dr. Y. Jeevan Nagendra Kumar


2. jQuery Fading Methods

With jQuery you can fade an element in and out of visibility.


jQuery has the following fade methods:
• fadeIn()
• fadeOut()
• fadeToggle()
• fadeTo()

a) jQuery fadeIn() Method

The jQuery fadeIn() method is used to fade in a hidden element.

Syntax:
$(selector).fadeIn(speed,callback);
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:

<!DOCTYPE html>
<html> <head>
<script src="[Link]"></script>
<script>
$(document).ready(function(){
$("button").click(function(){
$("#div1").fadeIn();
$("#div2").fadeIn("slow");
$("#div3").fadeIn(3000);
});
});
31

</script>
Page

</head>

FSD-Unit III Dr. Y. Jeevan Nagendra Kumar


<body>
<p>Demonstrate fadeIn() with different parameters.</p>
<button>Click to fade in boxes</button><br><br>

<div id="div1" style="width:80px;height:80px;display:none;background-


color:red;"></div><br>
<div id="div2" style="width:80px;height:80px;display:none;background-
color:green;"></div><br>
<div id="div3" style="width:80px;height:80px;display:none;background-
color:blue;"></div>

</body>
</html>

b) jQuery fadeOut() Method

The jQuery fadeOut() method is used to fade out a visible element.

Syntax:

$(selector).fadeOut(speed,callback);

$("button").click(function(){
$("#div1").fadeOut();
$("#div2").fadeOut("slow");
$("#div3").fadeOut(3000);
});

jQuery fadeToggle() Method


32

The jQuery fadeToggle() method toggles between the fadeIn() and fadeOut() methods.
Page

FSD-Unit III Dr. Y. Jeevan Nagendra Kumar


If the elements are faded out, fadeToggle() will fade them in.

If the elements are faded in, fadeToggle() will fade them out.

Syntax:

$(selector).fadeToggle(speed,callback);

Example:
$("button").click(function(){
$("#div1").fadeToggle();
$("#div2").fadeToggle("slow");
$("#div3").fadeToggle(3000);
});

c) jQuery fadeTo() Method

The jQuery fadeTo() method allows fading to a given opacity (value between 0 and 1).

Syntax:

$(selector).fadeTo(speed,opacity,callback);

Example:
$(document).ready(function(){
$("button").click(function(){
$("#div1").fadeTo("slow", 0.15);
$("#div2").fadeTo("slow", 0.4);
$("#div3").fadeTo("slow", 0.7);
});
});
33
Page

FSD-Unit III Dr. Y. Jeevan Nagendra Kumar


3. jQuery Sliding Methods

With jQuery you can create a sliding effect on elements.

jQuery has the following slide methods:

• slideDown()
• slideUp()
• slideToggle()

a) jQuery slideDown() Method

The jQuery slideDown() method is used to slide down an element.

Syntax:

$(selector).slideDown(speed,callback);

Example:

<!DOCTYPE html>
<html>
<head>
<script src="[Link]"></script>
<script>
$(document).ready(function(){
$("#flip").click(function(){
$("#panel").slideDown("slow");
});
});
</script>
<style>
#panel, #flip {
34

padding: 5px;
Page

FSD-Unit III Dr. Y. Jeevan Nagendra Kumar


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>

35
Page

FSD-Unit III Dr. Y. Jeevan Nagendra Kumar


b) jQuery slideUp() Method

The jQuery slideUp() method is used to slide up an element.

Syntax:

$(selector).slideUp(speed,callback);

Example:

$("#flip").click(function(){
$("#panel").slideUp();
});

c) jQuery slideToggle() Method

The jQuery slideToggle() method toggles between


the slideDown() and slideUp() methods.

If the elements have been slid down, slideToggle() will slide them up.

If the elements have been slid up, slideToggle() will slide them down.

$(selector).slideToggle(speed,callback);

$("#flip").click(function(){
$("#panel").slideToggle();
});
36
Page

FSD-Unit III Dr. Y. Jeevan Nagendra Kumar


4. jQuery - Animation

The jQuery animate() method is used to create custom animations.

Syntax:

$(selector).animate({params},speed,callback);

The required params parameter defines the CSS properties to be animated.

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 animation


completes.

The following example demonstrates a simple use of the animate() method; it moves a
<div> element to the right, until it has reached a left property of 250px:

<!DOCTYPE html>
<html>
<head>
<script src="[Link]"></script>
<script>
$(document).ready(function(){
$("button").click(function(){
$("div").animate({left: '250px'});
});
});
</script>
</head>
<body>

<button>Start Animation</button>
37
Page

FSD-Unit III Dr. Y. Jeevan Nagendra Kumar


<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:100px;position:absolute;"></div>

</body>
</html>

jQuery animate() - Manipulate Multiple Properties

Notice that multiple properties can be animated at the same time:

$("button").click(function(){
$("div").animate({
left: '250px',
opacity: '0.5',
height: '150px',
width: '150px'
});
});

jQuery animate() - Using Relative Values

It is also possible to define relative values (the value is then relative to the element's
current value). This is done by putting += or -= in front of the value:

$("button").click(function(){
$("div").animate({
left: '250px',
height: '+=150px',
width: '+=150px'
});
38

});
Page

FSD-Unit III Dr. Y. Jeevan Nagendra Kumar


jQuery animate() - Using Pre-defined Values

You can even specify a property's animation value as "show", "hide", or "toggle":

$(document).ready(function(){
$("button").click(function(){
$("div").animate({
height: 'toggle'
});
});
});

39
Page

FSD-Unit III Dr. Y. Jeevan Nagendra Kumar


jQuery 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 an HTML page as a tree (DOM tree). With jQuery traversing,
you can easily move up (ancestors), down (descendants) and sideways (siblings) in the
tree, starting from the selected (current) element. This movement is called traversing -
or moving through - the DOM tree.

• The <div> element is the parent of <ul>, and an ancestor of everything inside of
it
• The <ul> element is the parent of both <li> elements, and a child of <div>
• The left <li> element is the parent of <span>, child of <ul> and a descendant of
<div>
• The <span> element is a child of the left <li> and a descendant of <ul> and <div>
• The two <li> elements are siblings (they share the same parent)
• The right <li> element is the parent of <b>, child of <ul> and a descendant of
<div>
40

• The <b> element is a child of the right <li> and a descendant of <ul> and <div>
Page

FSD-Unit III Dr. Y. Jeevan Nagendra Kumar


1. jQuery Traversing - Ancestors

With jQuery you can traverse up the DOM tree to find ancestors of an element.
An ancestor is a parent, grandparent, great-grandparent, and so on.
Traversing Up the DOM Tree
Three useful jQuery methods for traversing up the DOM tree are:
• parent()
• parents()
• parentsUntil()

a) jQuery parent() Method

The parent() method returns the direct parent element of the selected element.
This method only traverse a single level up the DOM tree.
The following example returns the direct parent element of each <span> elements:
<!DOCTYPE html>
<html>
<head>
<style>
.ancestors * {
display: block;
border: 2px solid lightgrey;
color: lightgrey;
padding: 5px;
margin: 15px;
}
</style>
<script src="[Link]"></script>
<script>
$(document).ready(function(){
$("span").parent().css({"color": "red", "border": "2px solid red"});
});
</script>
41

</head>
Page

FSD-Unit III Dr. Y. Jeevan Nagendra Kumar


<body>
<div class="ancestors">
<div style="width:500px;">div (great-grandparent)
<ul>ul (grandparent)
<li>li (direct parent)
<span>span</span>
</li>
</ul>
</div>
<div style="width:500px;">div (grandparent)
<p>p (direct parent)
<span>span</span>
</p>
</div>
</div> </body> </html>

42
Page

FSD-Unit III Dr. Y. Jeevan Nagendra Kumar


b) jQuery parents() Method

The parents() method returns all ancestor elements of the selected element, all the way
up to the document's root element (<html>).
The following example returns all ancestors of all <span> elements:
$(document).ready(function(){
$("span").parents();
});

c) jQuery parentsUntil() Method

The parentsUntil() method returns all ancestor elements between two given arguments.
The following example returns all ancestor elements between a <span> and
a <div> element:
$(document).ready(function(){
$("span").parentsUntil("div");
}); 43
Page

FSD-Unit III Dr. Y. Jeevan Nagendra Kumar


2. jQuery Traversing - Descendants

With jQuery you can traverse down the DOM tree to find descendants of an element.
A descendant is a child, grandchild, great-grandchild, and so on.
Traversing Down the DOM Tree

Two useful jQuery methods for traversing down the DOM tree are:

• children()
• find()

a) jQuery children() Method

The children() method returns all direct children of the selected element.

This method only traverses a single level down the DOM tree.

The following example returns all elements that are direct children of
each <div> elements:

<!DOCTYPE html>
<html> <head>
<style>
.descendants * {
display: block;
border: 2px solid lightgrey;
color: lightgrey;
padding: 5px;
margin: 15px;
}
</style>
<script src="[Link]"></script>
<script>
44

$(document).ready(function(){
Page

$("div").children().css({"color": "red", "border": "2px solid red"});

FSD-Unit III Dr. Y. Jeevan Nagendra Kumar


});
</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>

45
Page

FSD-Unit III Dr. Y. Jeevan Nagendra Kumar


b) jQuery find() Method

The find() method returns descendant elements of the selected element, all the way
down to the last descendant.

The following example returns all <span> elements that are descendants of <div>:

<!DOCTYPE html>
<html>
<head>
<style>
.descendants * {
display: block;
border: 2px solid lightgrey;
color: lightgrey;
padding: 5px;
margin: 15px;
}
</style>
<script src="[Link]"></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>
46

<p>p (child)
Page

<span>span (grandchild)</span>

FSD-Unit III Dr. Y. Jeevan Nagendra Kumar


</p>
</div>

</body>
</html>

3. jQuery Traversing - Siblings

With jQuery you can traverse sideways in the DOM tree to find siblings of an element.

Siblings share the same parent.

Traversing Sideways in The DOM Tree

There are many useful jQuery methods for traversing sideways in the DOM tree:

• siblings()
• next()
• nextAll()
• nextUntil()
• prev()
47

• prevAll()
Page

• prevUntil()

FSD-Unit III Dr. Y. Jeevan Nagendra Kumar


a) jQuery siblings() Method

The siblings() method returns all sibling elements of the selected element.

The following example returns all sibling elements of <h2>:

<!DOCTYPE html>
<html> <head>
<style>
.siblings * {
display: block;
border: 2px solid lightgrey;
color: lightgrey;
padding: 5px;
margin: 15px;
}
</style>
<script src="[Link]"></script>
<script>
$(document).ready(function(){
$("h2").siblings().css({"color": "red", "border": "2px solid red"});
});
</script>
</head>
<body class="siblings">

<div>div (parent)
<p>p</p>
<span>span</span>
<h2>h2</h2>
<h3>h3</h3>
<p>p</p>
48

</div>
Page

</body> </html>

FSD-Unit III Dr. Y. Jeevan Nagendra Kumar


b) jQuery next() Method

The next() method returns the next sibling element of the selected element.

The following example returns the next sibling of <h2>:

$(document).ready(function(){
$("h2").next();
});

49
Page

FSD-Unit III Dr. Y. Jeevan Nagendra Kumar


c) jQuery nextAll() Method

The nextAll() method returns all next sibling elements of the selected element.

The following example returns all next sibling elements of <h2>:

$(document).ready(function(){
$("h2").nextAll();
});
jQuery nextUntil() Method
The nextUntil() method returns all next sibling elements between two given arguments.
The following example returns all sibling elements between a <h2> and a <h6> element:
$(document).ready(function(){
$("h2").nextUntil("h6");
});

d) jQuery prev(), prevAll() & prevUntil() Methods

The prev(), prevAll() and prevUntil() methods work just like the methods above but
50

with reverse functionality: they return previous sibling elements.


Page

FSD-Unit III Dr. Y. Jeevan Nagendra Kumar


jQuery Traversing - Filtering
The first(), last(), eq(), filter() and not() Methods

The most basic filtering methods are first(), last() and eq(), which allow you to select a
specific element based on its position in a group of elements.

Other filtering methods, like filter() and not() allow you to select elements that match,
or do not match, a certain criteria.

a) jQuery first() Method

The first() method returns the first element of the specified elements.

The following example selects the first <div> element:

<!DOCTYPE html>
<html>
<head>
<script src="[Link]"></script>
<script>
$(document).ready(function(){
$("div").first().css("background-color", "yellow");
});
</script>
</head>
<body>

<h1>Welcome to My Homepage</h1>

<p>This is a paragraph.</p>
51

<div style="border: 1px solid black;">


Page

FSD-Unit III Dr. Y. Jeevan Nagendra Kumar


<p>A paragraph in a div.</p>
<p>Another paragraph in a div.</p>
</div>
<br>
<div style="border: 1px solid black;">
<p>A paragraph in another div.</p>
<p>Another paragraph in another div.</p>
</div>
<br>

<div style="border: 1px solid black;">


<p>A paragraph in another div.</p>
<p>Another paragraph in another div.</p>
</div>

</body>
</html>

52
Page

FSD-Unit III Dr. Y. Jeevan Nagendra Kumar


b) jQuery last() Method

The last() method returns the last element of the specified elements.

The following example selects the last <div> element:

$(document).ready(function(){
$("div").last();
});

c) jQuery eq() method

The eq() method returns an element with a specific index number of the selected
elements.

The index numbers start at 0, so the first element will have the index number 0 and not
1. The following example selects the second <p> element (index number 1):

$(document).ready(function(){
$("p").eq(1);
53

});
Page

FSD-Unit III Dr. Y. Jeevan Nagendra Kumar


<!DOCTYPE html>
<html>
<head>
<script src="[Link]"></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>

54
Page

FSD-Unit III Dr. Y. Jeevan Nagendra Kumar


d) jQuery filter() Method

The filter() method lets you specify a criteria. Elements that do not match the criteria
are removed from the selection, and those that match will be returned.

The following example returns all <p> elements with class name "intro":

<!DOCTYPE html>
<html>
<head>
<script src="[Link]"></script>
<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>
55
Page

FSD-Unit III Dr. Y. Jeevan Nagendra Kumar


e) jQuery not() Method

The not() method returns all elements that do not match the criteria.

Tip: The not() method is the opposite of filter().

56
Page

FSD-Unit III Dr. Y. Jeevan Nagendra Kumar

You might also like