Chapter 5
Chapter 5
Chapter 5
Introduction to jQuery
Introduction
JavaScript is the most popular and the only scripting language for the front-end
development.
Before jQuery’s emergence, many routine tasks required several lines of code in
JavaScript.
After introduction of jQuery, those huge lines of JavaScript code were encompassed into
callable methods.
jQuery is an open-source JavaScript library.
jQuery is cross-platform and supports different types of browsers.
jQuery is a small, light-weight, cross-platform and feature-rich JavaScript library.
jQuery is designed to simplify the client-side scripting of HTML.
o The main purpose of jQuery is to provide an easy way to use JavaScript on the
website, so that the website is more interactive and attractive.
jQuery is a lightweight, "write less, do more", JavaScript library.
o jQuery takes a lot of common tasks that require many lines of JavaScript code to
accomplish, and wraps them into methods that can be called with a single line of
code whenever needed.
The jQuery library contains the following features:
o It helps us to manipulate HTML and CSS
o It helps us to manipulate DOM (Document Object Model) elements.
o HTML event methods
Provides event methods to trigger and respond to an events on a html page
such as mouse click, keypress etc.
o Implements AJAX calls.
o JSON Parsing
o used to add Effects and Animations
o jQuery has plugins for almost any task out there.
Configuration of jQuery
There are two basic approaches for incorporating the jQuery library on your website.
o Approach 1: Download the jQuery
o Approach 2: Include jQuery
<head>
<script src=“https://ajax.aspnetcdn.com/ajax/jQuery/jQuery-3.3.1.min.js”></script>
</head>
<head>
<script src=“https://ajax.googleapis.com/ajax/libs/jquery/3.3.1/jquery.min.js”></script>
</head>
Syntax
The jQuery syntax is tailor-made for selecting HTML elements and performing some
action on the element(s).
The standard format for a jQuery syntax is
$(selector).action()
o $ sign to define/access jQuery.
o selector that searches for the specified HTML elements.
o action() which executes a task on the selected elements.
For examples:
o $(this).hide() - hides the current element.
o $("p").hide() - hides all <p> elements.
o $(".test").hide() - hides all elements with class="test".
o $("#test").hide() - hides the element with id="test".
$(document).ready(function(){
// Any jQuery method will be defined here
});
$(function(){
// Any jQuery method will be defined here
});
Selectors
Element
<!DOCTYPE html>
<html>
<head>
<script
src="https://ajax.googleapis.com/ajax/libs/jquery/3.3.1/jquery.min.js">
</script>
<script>
$(document).ready(function() {
$("button").click(function() {
$("p1").hide();
});
});
</script>
</head>
<body>
<h2>This Example Demonstrates Element Selector</h2>
<p1>
The first paragraph is about to disappear by using jQuery. <br></br>
</p1>
<p2>The second paragraph is not going to disappear.</p2>
#id Selector
The jQuery id selector uses the id attribute of an HTML tag to find the specific element.
An id is always unique (in a single webpage). Hence, the id selector is needed to search
for a unique element.
To use this selector, write a hash character before the element id.
For example
<!DOCTYPE html>
<html>
<head>
<script
src="https://ajax.googleapis.com/ajax/libs/jquery/3.3.1/jquery.min.js">
</script>
<script>
$(document).ready(function() {
$("button").click(function() {
$("#second").hide();
});
});
</script>
</head>
<body>
<h2>This Example Demonstrates ID Selector</h2>
<p1>
The first paragraph is about to disappear by using jQuery. <br>
</p1>
<p2 id="second">
The second paragraph which is not going to disappear.<br><br>
</p2>
Class Selector
The jQuery use the class selector for finds any HTML element with a defined class.
To find elements with a specific class, write a period character, followed by the name of
the class.
For example
<!DOCTYPE html>
<html>
<head>
<script
src="https://ajax.googleapis.com/ajax/libs/jquery/3.3.1/jquery.min.js">
</script>
<script>
$(document).ready(function() {
$("button").click(function() {
$(".hd").hide();
});
});
</script>
</head>
<body>
<h2 class="hd">This Example Demonstrates Class Selector</h2>
<p1>The first paragraph is not going to disappear by using jQuery.
<br><br>
</p1>
<p2 class="hd">
The second paragraph is going to disappear.<br><br>
</p2>
<button>
Click Here to Disappear the Second Paragraph With Heading
</button>
</body>
</html>
jQuery Events
Whenever a visitor performs an action on an HTML page, there are pre-defined replies to
provide them with a response. This pre-defined reply is called as an event.
jQuery is also used to create an “event” in a webpage.
Event is like a response which is triggered by a user action. jQuery provides methods to
capture these user actions and reply to them.
An event can be triggered when a user
o clicks on the submit button, or
o checks a radio-button, or
o even if they simply hover a mouse around an element.
The triggering state of an event is often characterized by the term “fire”.
o The keypress event is fired, the moment user press a key.
click() method
<!DOCTYPE html>
<html>
<head>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.3.1/jquery.min.js">
</script>
<script>
$(document).ready(function(){
/*This tells jQuery to execute the code in this block when a click
event occurs on h1, h2, or h3 elements*/
$("h1, h2, h3").click(function(){
$(this).hide();
});
});
</script>
</head>
<body>
<h1></h1>
<h1>If click on First Heading, It will disappear.</h1>
<h2>If click on Second Heading, It will disappear.</h2>
<h3>If click on Third Heading, It will disappear.</h3>
<p>Use single click on heading to hide it.</p>
</body>
</html>
dblclick() method
<!DOCTYPE html>
<html>
<head>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.3.1/jquery.min.js">
</script>
<script>
$(document).ready(function(){
/*This tells jQuery to execute the code in this block when a click
event occurs on h1, h2, or h3 elements*/
$("h1,h2,h3").dblclick(function(){
$(this).hide();
});
});
</script>
</head>
<body>
<h1></h1>
mouseenter() method
The mouseenter() method is used for integrating an event handling functionality with an
HTML element.
The function is executed when the mouse pointer enters the HTML element.
For example
<html>
<head>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.3.1/jquery.min.js">
</script>
<script>
$(document).ready(function(){
$(".a").mouseenter(function(){
alert("Your cursor touched the HTML elements.");
});
});
</script>
</head>
<body>
<h1 class="a">Example of Mouseenter</h1>
<p class="a">Take the cursor to the HTML element.</p>
</body>
</html>
mouseleave() method
<!DOCTYPE html>
<html>
<head>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.3.1/jquery.min.js">
</script>
<script>
$(document).ready(function(){
$("#par").mouseleave(function(){
alert("You left the paragraph element!");
});
});
</script>
</head>
<body>
<p id="par">Take the cursor to the HTML element.</p>
</body>
</html>
jQuery Effects
In jQuery, HTML elements can appear and disappear by using the hide() and show() methods.
For example
<html>
<head>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.3.1/jquery.min.js">
</script>
<script>
$(document).ready(function(){
/* This captures the click event on element with id "a" */
$("#a").click(function(){
/* This hides all the elements p */
$("p").hide();
});
The parentheses of both hide() and show() function can take two optional arguments:
speed and callback.
Syntax:
o $(selector).hide(speed,callback);
o $(selector).show(speed,callback);
The speed parameter specifies the speed of the hiding/showing, and can be defined by
values: "slow", "fast", or milliseconds (digits).
The callback argument performs its task when the show or hide method finishes.
The following example illustrate that the HTML elements disappear after 3 seconds.
<!DOCTYPE html>
<html>
<head>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.3.1/jquery.min.js">
</script>
<script>
$(document).ready(function(){
$("button").click(function(){
$(".a").hide(3000);
});
});
</script>
</head>
<body>
<button>Hide</button>
<p class="a">These paragraphs and the single heading will hide 3 seconds
after:</p>
<p class="a">A user clicks on the button. </p>
</body>
</html>
toggle() methods
The toggle() method is defined to toggle between hiding and showing HTML element.
Here, the shown elements are hidden and hidden elements are shown by using same
button.
For example
<!DOCTYPE html>
<html>
<head>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.3.1/jquery.min.js">
</script>
<script>
$(document).ready(function(){
$("button").click(function(){
$(".a").toggle();
});
});
</script>
</head>
<body>
<button>
Hide or Show HTML elements by using this Toggle Button!
</button>
<h1 class ="a">Toggle Example </h1>
<p class="a">Whenever, the toggle button is hit,</p>
<p class="a">it would hide and show the paragraphs. </p>
</body>
</html>
fadeIn() method
This method is useful to change the opacity of an element from hidden to visible.
For example
<!DOCTYPE html>
<html>
<head>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.3.1/jquery.min.js">
</script>
<script>
$(document).ready(function(){
$("button").click(function(){
$("#1").fadeIn(2000);
$("#2").fadeIn(4000);
$("#3").fadeIn(7000);
});
});
</script>
</head>
<body>
<p>Applying fadeIn effect on each box with different timing.</p>
fadeOut() method
This method is useful to change the opacity of an element from visible to hidden.
For example
<!DOCTYPE html>
<html>
<head>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.3.1/jquery.min.js">
</script>
<script>
$(document).ready(function(){
$("button").click(function(){
$("#1").fadeOut(2000);
$("#2").fadeOut(4000);
$("#3").fadeOut(7000);
});
});
</script>
</head>
<body>
<p>Applying fadeOut effect on each box with different timing.</p>
<button>Hit the button to get the fadingOut effect.</button><br><br>
fadeToggle() method
The jQuery fadeToggle() method toggles between the fadeIn() and fadeOut() methods.
If the elements are faded out, fadeToggle() will fade them in.
If the elements are faded in, fadeToggle() will fade them out.
For example
<!DOCTYPE html>
<html>
<head>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.3.1/jquery.min.js">
</script>
<script>
$(document).ready(function(){
$("button").click(function(){
$("#1").fadeToggle(2000);
$("#2").fadeToggle(4000);
$("#3").fadeToggle(7000);
});
});
</script>
</head>
<body>
<p>Applying fadeToggle effect on each box with different timing.</p>
<button>
Hit the button to get the fadingIn/fadingOut effect.
</button><br><br>
fadeTo() Method
The jQuery fadeTo() method allows fading to a given opacity (value between 0 and 1).
For example
<!DOCTYPE html>
<html>
<head>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.6.0/jquery.min.js">
</script>
<script>
$(document).ready(function(){
$("button").click(function(){
$("#1").fadeTo(2000, 0.15);
$("#2").fadeTo(3000, 0.4);
$("#3").fadeTo(4000, 0.6);
});
});
</script>
</head>
<body>
<p>Applying fadeTo effect on each box with different timing.</p>
slideDown() Method
<!DOCTYPE html>
<html>
<head>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.3.1/jquery.min.js">
</script>
<script>
$(document).ready(function(){
$("#flip").click(function(){
$("#panel").slideDown("fast");
});
});
</script>
<style>
#panel, #flip {
padding: 10px;
text-align: left;
background-color: dodgerblue;
border: solid 5px purple;
}
#panel {
padding: 60px;
display: none;
}
</style>
</head>
<body>
<div id="flip">When this area is clicked, a panel emerges.</div>
<div id="panel">The panel greets the user with Merry Christmas!</div>
</body>
</html>
slideUp() Method
This method is useful to sliding up an element from bottom to top, which gives animated
effect of concealing.
For example
<!DOCTYPE html>
<html>
<head>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.3.1/jquery.min.js">
</script>
<script>
$(document).ready(function(){
$("#flip").click(function(){
$("#panel").slideUp("slow");
});
});
</script>
<style>
#panel, #flip {
padding: 10px;
text-align: left;
background-color: dodgerblue;
border: solid 5px purple;
}
#panel {
padding: 60px;
}
</style>
</head>
<body>
<div id="flip">When this area is clicked, a panel emerges.</div>
<div id="panel">The panel greets the user with Merry Christmas!</div>
</body>
</html>
slideToggle() Method
<!DOCTYPE html>
<html>
<head>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.3.1/jquery.min.js">
</script>
<script>
$(document).ready(function(){
$("#flip").click(function(){
$("#panel").slideToggle("slow");
});
});
</script>
<style>
#panel, #flip {
padding: 10px;
text-align: left;
background-color: dodgerblue;
border: solid 5px purple;
}
#panel {
padding: 60px;
display: none;
}
</style>
</head>
<body>
<div id="flip">When this area is clicked, a panel emerges.</div>
<div id="panel">The panel greets the user with Merry Christmas!</div>
</body>
</html>
<!DOCTYPE html>
<html>
<head>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.3.1/jquery.min.js">
</script>
<script>
$(document).ready(function(){
$("button").click(function(){
$("div").animate({left: '500px'});
});
});
</script>
</head>
<body>
<button>Move the box!</button>
<p></p>
<div style="background:dodgerblue;height:50px;width:50px;position:fixed;">
</div>
</body>
</html>
For example
<!DOCTYPE html>
<html>
<head>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.3.1/jquery.min.js">
</script>
<script>
$(document).ready(function(){
$("button").click(function(){
$("div").animate({
left: '500px',
opacity: '0.2',
height: '100px',
width: '100px'
});
});
});
</script>
</head>
<body>
<button>Begin the Box Animation</button><br><br>
<div style="background:dodgerblue;height:50px;width:50px;position:fixed;">
</div>
</body>
</html>
The hide, show, and toggle values can be used in the animate method.
For example
<!DOCTYPE html>
<html>
<head>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.3.1/jquery.min.js">
</script>
<script>
$(document).ready(function(){
$("button").click(function(){
$("div").animate({
width: 'toggle'
});
});
});
</script>
</head>
<body>
<p>Click the button repeatedly to check its impact.</p>
<button>Toggle the Animation</button><br><br>
<div style="background:dodgerblue;height:50px;width:50px;position:absolute;">
</div>
</body>
</html>
<!DOCTYPE html>
<html>
<head>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.3.1/jquery.min.js">
</script>
<script>
$(document).ready(function(){
$("button").click(function(){
var div = $("div");
div.animate({height: '50px', opacity: '0.3'}, "slow");
div.animate({width: '300px', opacity: '0.7'}, "fast");
div.animate({height: '50px', opacity: '0.3'}, "fast");
div.animate({width: '300px', opacity: '0.7'}, "slow");
div.animate({fontSize: '2em'}, "slow");
});
});
</script>
</head>
<body>
<button>Start Animation</button><br><br>
<div style="background:dodgerblue;height:50px;width:50px;position:absolute;">
Animation</div>
</body>
</html>
When an animation or effect is still in process, then the stop() method can halt it before
its completion.
This method is applicable to fading, sliding, custom animations, and any effect functions.
Syntax
$(selector).stop(stopAll, goToEnd);
o These parameters are optional.
o The “stopAll” parameter is used to specify if the queue in the animation requires
to be cleared.
o By default, “stopAll” is false; therefore, only the animation which is active is
blocked, thereby permitting other animations in the queue to be executed.
o The “goToEnd” parameter defines if the current animation should be finished
quickly. By default, it is false.
The stop() method stops any current animation, depending upon the element which is
selected.
For example
<!DOCTYPE html>
<html>
<head>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.3.1/jquery.min.js">
</script>
<script>
$(document).ready(function(){
$("#flip").click(function(){
$("#panel").slideDown(2000);
});
$("#stop").click(function(){
$("#panel").stop();
});
});
</script>
<style>
#panel, #flip {
padding: 3px;
font-size: 50px;
text-align: left;
background-color: green;
color: black;
border: solid 1px #666;
}
#panel {
padding: 20px;
display: none;
}
</style>
</head>
<body>
<button id="stop">Pause the sliding effect</button><br><br>
<div id="flip">Hit this area for sliding down the panel.</div>
<div id="panel">Hello User! </div>
</body>
</html>
<!DOCTYPE html>
<html>
<head>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.3.1/jquery.min.js">
</script>
<script>
$(document).ready(function(){
$("button").click(function(){
$("h1").hide(3000, function(){
alert("The heading has disappeared.");
});
});
});
</script>
</head>
<body>
<button>Make the heading disappear!</button>
<h1>Callback Function</h1>
</body>
</html>
jQuery Chaining
<!DOCTYPE html>
<html>
<head>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.3.1/jquery.min.js">
</script>
<script>
$(document).ready(function(){
$("button").click(function(){
$(".h").css("color", "blue").slideUp(2000).slideDown(3000);
});
});
</script>
</head>
<body>
<h1 class="h">Using chaining to modify this heading.</h1>
<h2 class="h">Using chaining to modify this heading.</h2>
<button>Hit Me!</button>
</body>
</html>
There are various DOM properties around but following are some of the more significant
ones. Let us say “n” is a node object.
o n.innerHTML: This property refers to the inner text value of an HTML element
“n”. However, note that it parses content as HTML so it takes longer to get value
compared to nodeValue property. For example, say “n” refers to <a> element like
<a href=“www.mayurramgir.com”>Mayur Ramgir</a>. In this case,
n.innerHTML will give “Mayur Ramgir”.
Get Content
The following are three basic and easy-to-use methods in jQuery for DOM manipulation:
o text() method: It is used to assign or return any piece of text for the selected
HTML elements.
o html() method: It is used to assign or return the content for the selected elements
(markup included).
o val() method: It is used to assign or return any value from the fields in the HTML
forms.
The following example demonstrates how to get content with the jQuery text() and html()
methods:
<!DOCTYPE html>
<html>
<head>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.3.1/jquery.min.js">
</script>
<script>
$(document).ready(function(){
$("#b1").click(function(){
alert("Text: " + $("#par").text());
});
$("#b2").click(function(){
alert("HTML: " + $("#par").html());
});
});
</script>
</head>
<body>
<p id="par">
This is line is part of a paragraph <b>and contains bold text.</b>
</p>
The following example demonstrates how to get the value of an input field with the
jQuery val() method.
<!DOCTYPE html>
<html>
<head>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.3.1/jquery.min.js">
</script>
<script>
$(document).ready(function(){
$("#button1").click(function(){
alert("Value: " + $("#txt").val());
});
});
</script>
</head>
<body>
<p>Username: <input type="text" id="txt" value="Ghost Rider"></p>
Get Attributes
To receive the value of HTML attributes, the jQuery attr() method is used to get attribute
values.
The following example demonstrates how to get the value of the href attribute in a link by
using the jQuery attr() method.
<!DOCTYPE html>
<html>
<head>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.3.1/jquery.min.js">
</script>
<script>
$(document).ready(function(){
$("button").click(function(){
alert($("#go").attr("href"));
});
});
</script>
</head>
<body>
<p>
<a href="https://www.soa.ac.in" id="go">SOA Deemed to be
University</a>
</p>
Set Content
<!DOCTYPE html>
<html>
<head>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.3.1/jquery.min.js">
</script>
<script>
$(document).ready(function(){
$("#b1").click(function(){
$("#par1").text("Hi User!");
});
$("#b2").click(function(){
$("#par2").html("<b>Hello World!</b>");
});
$("#b3").click(function(){
$("#par3").val("Shane");
});
});
</script>
</head>
<body>
<p id="par1">This is our first paragraph.</p>
Set Attributes
To change or modify the values of an attribute, the jQuery attr() method is used.
The following example demonstrates how to change (set) the value of the href attribute in
a link, so that the modified the value of href is redirect a user to another website.
For example
<!DOCTYPE html>
<html>
<head>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.3.1/jquery.min.js">
</script>
<script>
$(document).ready(function(){
$("#b1").click(function(){
$("#w3s").attr("href", "https://www.soa.ac.in");
});
});
</script>
</head>
<body>
<p id="par1">
<a href="https://www.google.com" id="w3s">Google.com</a>
</p>
<p>After clicking the link, place your cursor on the hyperlink and check
the link address or click it to go to other website.</p>
</body>
</html>
Addition of Elements
For the addition of fresh contents in HTML elements, jQuery provides four methods:
o append() method
o prepend() method
o after() method
o before() method
append() method
The append() method is used, in order to insert any piece of content at the extreme end of
the element.
In the following example, demonstrate to appended text at the end of the paragraphs as
well attached one more list item to the end of the list.
For example
<!DOCTYPE html>
<html>
<head>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.3.1/jquery.min.js">
</script>
<script>
$(document).ready(function(){
$("#b1").click(function(){
$("p").append(" <b>New Content</b>.");
});
$("#b2").click(function(){
$("ul").append("<li>Soup</li>");
});
});
</script>
</head>
<body>
<p>This is the first paragraph.</p>
<p>This is the second paragraph.</p>
<ul>
<li>Coffee</li>
<li>Tea</li>
<li>Water</li>
</ul>
prepend() method
<!DOCTYPE html>
<html>
<head>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.3.1/jquery.min.js">
</script>
<script>
$(document).ready(function(){
$("#b1").click(function(){
$("p").prepend("<b>New Content</b>. ");
});
$("#b2").click(function(){
$("ol").prepend("<li>Nuggets</li>");
});
});
</script>
</head>
<body>
<p>This is the first paragraph.</p>
<ol>
<li>Pizza</li>
<li>Burger</li>
<li>Sandwich</li>
</ol>
The jQuery after() method inserts content AFTER the specified HTML elements.
The jQuery before() method inserts content BEFORE the specified HTML elements.
The following example demonstrates to add content before and after an image.
<!DOCTYPE html>
<html>
<head>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.3.1/jquery.min.js">
</script>
<script>
$(document).ready(function(){
$("#b1").click(function(){
$("img").before("<b>Look at the following
image.</b><br><br>");
});
$("#b2").click(function(){
$("img").after("<br><br>the above image is beautiful.");
});
});
</script>
</head>
<body>
<img src="photography.jpg" alt="jQueryimg" width="150"
height="100"><br><br>
</html>
remove() method
jQuery remove() method, delete any selected single element or set of elements.
It also deletes the child elements of those elements.
For example
<!DOCTYPE html>
<html>
<head>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.3.1/jquery.min.js">
</script>
<script>
$(document).ready(function(){
$("button").click(function(){
$("#d").remove();
});
});
</script>
</head>
<body>
<div id="d" style="height:500px;width:500px;border:3px solid
green;background-color:orange;">
We are using div element in our example.
<p>A button is used for deletion.</p>
<p>The button calls out the remove() method to delete the
element.</p>
</div>
<br>
<button>Delete the div element from the page.</button>
</body>
</html>
The remove() method can also take one parameter for filtering of elements and remove.
<!DOCTYPE html>
<html>
<head>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.3.1/jquery.min.js">
</script>
<script>
$(document).ready(function(){
$("button").click(function(){
$("p").remove(".b");
});
});
</script>
<style>
.b {
color: dodgerblue;
font-size: 40px;
}
</style>
</head>
<body>
<p class ="a"><b>This is the first paragraph.<b></p>
<p class="b">This is the second paragraph.</p>
<p class="b">This is the third paragraph.</p>
<button>Remove all p elements with class="b"</button>
</body>
</html>
empty() Method
<!DOCTYPE html>
<html>
<head>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.3.1/jquery.min.js">
</script>
<script>
$(document).ready(function(){
$("button").click(function(){
$("#d").empty();
});
});
</script>
</head>
<body>
<div id="d" style="height:500px;width:500px;border:3px solid green;
background-color:orange;">
We are using div element in our example.
<p>A button is used for deletion.</p>
<p>The button calls out the remove() method to delete the element.</p>
</div>
<br>
<button>Delete the div element from the page.</button>
</body>
</html>
addClass() method
By using addClass(), CSS class attributes can be added in various HTML elements.
You can pick a single or multiple elements and assign a class with it.
In the following example, assigned a CSS class “green” to change color of first heading
and paragraph. Similarly, by using a “size” class, modified the size of div element.
<!DOCTYPE html>
<html>
<head>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.3.1/jquery.min.js">
</script>
<script>
$(document).ready(function(){
$("#b1").click(function(){
$("h1, p").addClass("green");
$("div").addClass("size");
});
});
</script>
<style>
.size {
font-size: xx-large;
}
.green {
color: green;
}
</style>
</head>
<body>
<h1>The is the first heading!</h1>
removeClass() method
To delete any selected class attribute from multiple elements, use the removeClass()
method.
The following example shows how to remove a specific class attribute from different
elements.
<!DOCTYPE html>
<html>
<head>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.3.1/jquery.min.js">
</script>
<script>
$(document).ready(function(){
$("button").click(function(){
$("h1, h2, p").removeClass("green");
});
});
</script>
<style>
.green {
color: green;
}
</style>
</head>
<body>
<h1 class="green">First Green Heading</h1>
<h2 class="green">Second Green Heading</h2>
toggleClass() method
The jQuery toggleClass() method, toggles between adding/removing classes from the
selected HTML’s elements.
The following example will show how to use the jQuery toggleClass() method.
<!DOCTYPE html>
<html>
<head>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.3.1/jquery.min.js">
</script>
<script>
$(document).ready(function(){
$("button").click(function(){
$("h1, p").toggleClass("green");
});
});
</script>
<style>
.green{
color: green;
}
</style>
</head>
<body>
<h1>This is the first heading which is soon to be turned green!</h1>
<h2>This is the second heading which is going to remain the same!</h2>
css() method
In order to set or return style properties for the specified elements, the css() method is
used.
Syntax:
css("propertyname");
In the following example, each paragraph has been assigned with a separate background
color. The css("background-color") method is used to show the RGB values of each
paragraph’s background color.
<!DOCTYPE html>
<html>
<head>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.3.1/jquery.min.js">
</script>
<script>
$(document).ready(function(){
$("button").click(function(){
alert("Background color = " + $("#p1").css("background-color"));
alert("Background color = " + $("#p2").css("background-color"));
alert("Background color = " + $("#p3").css("background-color"));
});
});
</script>
</head>
<body>
<h2>This is a heading</h2>
The following example will set the background-color value for ALL matched elements:
<!DOCTYPE html>
<html>
<head>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.3.1/jquery.min.js">
</script>
<script>
$(document).ready(function(){
$("button").click(function(){
$("#p1").css("background-color", "red");
$("#p2").css("background-color", "blue");
$("#p3").css("background-color", "green");
});
});
</script>
</head>
<body>
<h2>How to set a CSS property? </h2>
</p>
<p id="p3" style="background-color:yellow">
This is the third paragraph.
</p>
<p>This paragraph is without any color.</p>
Traversing
jQuery provides excellent traversing functionality.
jQuery traversing, which means "move through", are used to "find" (or select) HTML
elements based on their relation to other elements.
It starts with one selection and moves through that selection until to reach the intended
(desire) element.
With jQuery traversing, you can easily move up (ancestors), down (descendants) and
sideways (siblings) in the tree, starting from the selected (current) element.
o An ancestor is a parent, grandparent, great-grandparent, and so on.
o A descendant is a child, grandchild, great-grandchild, and so on.
o The Siblings share the same parent.
The Figure 5.1 shows an example of HTML page as a DOM tree.
o It has a root element named <html>.
o The <html> element contains two main elements <head> and <body>.
o The <head> contains header related information like page title, meta data, etc.
o The <body> contains all the elements which will be displayed on a browser
window.
o Every element has various attributes and most of the elements may also contain
text.
o See the following <button> element example which has attribute “type” and text
which can be specified by the developer.
Ancestors
parent() method
The parent() method returns the direct parent element of the selected element.
This method only traverses a single level up the DOM tree.
The following example returns the direct parent element of each <span> element.
<!DOCTYPE html>
<html>
<head>
<style>
.outermost * {
display: block;
border: 2px solid lightgrey;
color: green;
padding: 6px;
margin: 20px;
}
</style>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.3.1/jquery.min.js">
</script>
<script>
$(document).ready(function(){
$("span").parent().css({"color": "blue", "border": "1px solid blue"});
});
</script>
</head>
<body>
<div class="outermost">
<div style="width:400px;">div (great-grandparent)
<ol>ol (grandparent)
<li>li (direct parent)
<span>rectangle 1</span>
</li>
</ol>
</div>
parents() method
The parents() method can be used to display all the ancestors of the selected element, all
the way up to the document's root element (<html>).
The following example returns all ancestors of <span> element.
<!DOCTYPE html>
<html>
<head>
<style>
.outermost * {
display: block;
border: 3px solid orange;
color: green;
padding: 6px;
margin: 18px;
}
</style>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.3.1/jquery.min.js">
</script>
<script>
$(document).ready(function(){
$("span").parents().css({"color": "blue", "border": "3px solid blue"});
});
</script>
</head>
</li>
</ul>
</div>
</body>
</html>
parentsUntil()
The parentsUntil() method is used to search for all the ancestors till a specific point.
The parentsUntil() method returns all ancestor elements between two given arguments.
The following example returns all parents starting from the <span> tag and ending at the
<div> tag.
<!DOCTYPE html>
<html>
<head>
<style>
.outermost * {
display: block;
border: 2px solid orange;
color: lightgrey;
padding: 6px;
margin: 18px;
}
</style>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.3.1/jquery.min.js">
</script>
<script>
$(document).ready(function(){
$("span").parentsUntil("div").css({"color": "blue", "border": "2px
solid blue"});
});
</script>
</head>
</body>
</html>
Descendants
jQuery helps to traverse down the DOM tree to find descendants of an element.
A descendant is a child or grandchild or great-grandchild of an element.
There are two main methods to find descendants:
o children() method
o find() method
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 the direct child of the div tag and represent by blue-color.
<!DOCTYPE html>
<html>
<head>
<style>
.innermost * {
display: block;
border: 1px solid green;
color: lightgreen;
padding: 6px;
margin: 18px;
}
</style>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.3.1/jquery.min.js">
</script>
<script>
$(document).ready(function(){
$("div").children().css({"color": "blue", "border": "3px solid red"});
});
</script>
</head>
<body>
<div class="innermost" style="width:300px;">div (current element)
<p>p (child)
<span> rectangle 1 (grandchild)</span>
</p>
<p>p (child)
<span> rectangle 2 (grandchild)</span>
</p>
</div>
</body>
</html>
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 which are child to <div> element and
css color properties.
<!DOCTYPE html>
<html>
<head>
<style>
.innermost * {
display: block;
border: 3px solid green;
color: lightgreen;
padding: 6px;
margin: 18px;
}
</style>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.3.1/jquery.min.js">
</script>
<script>
$(document).ready(function(){
$("div").find("span").css({"color": "blue", "border": "3px solid blue"});
});
</script>
</head>
<body>
<div class="innermost" style="width:300px;">div (current element)
<p>p (child)
<span>rectangle 1 (grandchild)</span>
</p>
<p>p (child)
<span>rectangle 2 (grandchild)</span>
</p>
</div>
</body>
</html>
<!DOCTYPE html>
<html>
<head>
<style>
.innermost * {
display: block;
border: 3px solid green;
color: lightgreen;
padding: 6px;
margin: 18px;
}
</style>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.3.1/jquery.min.js">
</script>
<script>
$(document).ready(function(){
$("div").find("*").css({"color": "blue", "border": "3px solid blue"});
});
</script>
</head>
<body>
<div class="innermost" style="width:300px;">div (current element)
<p>p (child)
<span>rectangle 1 (grandchild)</span>
</p>
<p>p (child)
<span>rectangle 2 (grandchild)</span>
</p>
</div>
</body>
</html>
Siblings
Siblings elements are the ones who share the same parent.
jQuery allows to traverse horizontally to search an element’s siblings.
To check a sibling, there are multiple methods for traversing sideways in the DOM tree.
o siblings() method
o next() method
o nextAll() method
o nextUntil() method
o prev() method
o prevAll() method
o prevUntil() method
siblings() method
The siblings() method returns all sibling elements of the selected element.
The following example returns all sibling elements of <h2> element.
<!DOCTYPE html>
<html>
<head>
<style>
.s * {
display: block;
border: 3px solid yellow;
color: orange;
padding: 6px;
margin: 18px;
}
</style>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.3.1/jquery.min.js">
</script>
<script>
$(document).ready(function(){
$("h2").siblings().css({"color": "blue", "border": "3px solid blue"});
});
</script>
</head>
<body class="s">
<div>div (parent)
<p>para 1</p>
<span>rectangle</span>
<h2>h2</h2>
<h3>h3</h3>
<p>para 2</p>
</div>
</body>
</html>
next() method
The next() method returns the next sibling element of the selected element.
The following example returns the next sibling of <h2> and <h3> element.
<!DOCTYPE html>
<html>
<head>
<style>
.s * {
display: block;
border: 3px solid green;
color: green;
padding: 6px;
margin: 18px;
}
</style>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.3.1/jquery.min.js">
</script>
<script>
$(document).ready(function(){
$("h2").next().css({"color": "blue", "border": "3px solid blue"});
});
$(document).ready(function(){
$("h3").next().css({"color": "blue", "border": "3px solid blue"});
});
</script>
</head>
<body class="s">
<div>div (parent)
<p>para 1</p>
<span>rectangle</span>
<h2>h2</h2>
<h3>h3</h3>
<p>para 2</p>
</div>
</body>
</html>
nextAll() method
The nextAll() method returns all next sibling elements of the selected element.
The following example returns all next sibling elements of <h2> element.
<!DOCTYPE html>
<html>
<head>
<style>
.s * {
display: block;
border: 3px solid green;
color: green;
padding: 6px;
margin: 18px;
}
</style>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.3.1/jquery.min.js">
</script>
<script>
$(document).ready(function(){
$("h2").nextAll().css({"color": "blue", "border": "3px solid blue"});
});
</script>
</head>
<body class="s">
<div>div (parent)
<p>para 1</p>
<p>para 2</p>
<span>rectangle</span>
<h2>h2</h2>
<h3>h3</h3>
<span>rectangle</span>
<p>para 3</p>
</div>
</body>
</html>
nextUntil() method
The nextUntil() method is used to view the next siblings of element till a given point is
reached.
The following example started searching for the next siblings of <h1> till <h5> element.
<!DOCTYPE html>
<html>
<head>
<style>
.s * {
display: block;
border: 3px solid green;
color: lightgrey;
padding: 6px;
margin: 18px;
}
</style>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.3.1/jquery.min.js">
</script>
<script>
$(document).ready(function(){
$("h1").nextUntil("h5").css({"color": "blue", "border": "3px solid blue"});
});
</script>
</head>
<body class="s">
<div>div (parent)
<p>para</p>
<span>rectangle</span>
<h1>h1</h1>
<h2>h2</h2>
<h3>h3</h3>
<h4>h4</h4>
<h5>h5</h5>
<h6>h6</h6>
<h6>h7</h6>
</div>
</body>
</html>
prev() method
<!DOCTYPE html>
<html>
<head>
<style>
.s * {
display: block;
border: 3px solid green;
color: lightgrey;
padding: 6px;
margin: 18px;
}
</style>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.3.1/jquery.min.js">
</script>
<script>
$(document).ready(function(){
$("h5").prev().css({"color": "blue", "border": "3px solid blue"});
});
</script>
</head>
<body class="s">
<div>div (parent)
<p>para</p>
<span>rectangle</span>
<h1>h1</h1>
<h2>h2</h2>
<h3>h3</h3>
<h4>h4</h4>
<h5>h5</h5>
<h6>h6</h6>
<h6>h7</h6>
</div>
</body>
</html>
prevAll() method
The prevAll() method is used to check all the previous elements of a sibling.
The following example checks all the previous siblings of <h5> element.
<!DOCTYPE html>
<html>
<head>
<style>
.s * {
display: block;
border: 3px solid green;
color: lightgrey;
padding: 6px;
margin: 18px;
}
</style>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.3.1/jquery.min.js">
</script>
<script>
$(document).ready(function(){
$("h5").prevAll().css({"color": "blue", "border": "3px solid blue"});
});
</script>
</head>
<body class="s">
<div>div (parent)
<p>para</p>
<span>rectangle</span>
<h1>h1</h1>
<h2>h2</h2>
<h3>h3</h3>
<h4>h4</h4>
<h5>h5</h5>
<h6>h6</h6>
<h6>h7</h6>
</div>
</body>
</html>
prevUntil() method
The prevUntil() method is used to view the next previous of element to till a given point
is reached.
The following example started searching for the previous siblings of <h5> to till <span>
element.
<!DOCTYPE html>
<html>
<head>
<style>
.s * {
display: block;
border: 3px solid green;
color: lightgrey;
padding: 6px;
margin: 18px;
}
</style>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.3.1/jquery.min.js">
</script>
<script>
$(document).ready(function(){
$("h5").prevUntil("span").css({"color": "blue", "border": "3px
solid blue"});
});
</script>
</head>
<body class="s">
<div>div (parent)
<p>para</p>
<span>rectangle</span>
<h1>h1</h1>
<h2>h2</h2>
<h3>h3</h3>
<h4>h4</h4>
<h5>h5</h5>
<h6>h6</h6>
<h6>h7</h6>
</div>
</body>
</html>
Filtering
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="https://ajax.googleapis.com/ajax/libs/jquery/3.3.1/jquery.min.js">
</script>
<script>
$(document).ready(function(){
$("div").first().css("background-color", "lightgreen");
});
</script>
</head>
<body>
<h1>How To Use First Method for Traversing?</h1>
<p>This paragraph does not belong to any div element.</p>
<div style="border: 3px solid blue;">
<p>This is the first paragraph in this div.</p>
<p>This is the second paragraph in this div.</p>
</div>
<br>
<div style="border: 3px solid blue;">
<p>This is the first paragraph in this div.</p>
<p>This is the second paragraph in this div.</p>
</div>
<br>
<div style="border: 3px solid blue;">
<p>This is the first paragraph in this div.</p>
<p>This is the second paragraph in this div.</p>
</div>
</body>
</html>
last() method
The last() method returns the ending element of the selected elements.
The following example selects the last <div> element.
<!DOCTYPE html>
<html>
<head>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.3.1/jquery.min.js">
</script>
<script>
$(document).ready(function(){
$("div").last().css("background-color", "lightgreen");
});
</script>
</head>
<body>
<h1>How To Use Last Method for Traversing?</h1>
<p>This paragraph does not belong to any div element.</p>
<div style="border: 3px solid blue;">
<p>This is the first paragraph in this div.</p>
<p>This is the second paragraph in this div.</p>
</div>
<br>
<div style="border: 3px solid blue;">
<p>This is the first paragraph in this div.</p>
<p>This is the second paragraph in this div.</p>
</div>
<br>
<div style="border: 3px solid blue;">
<p>This is the first paragraph in this div.</p>
<p>This is the second paragraph in this div.</p>
</div>
</body>
</html>
eq() method
<!DOCTYPE html>
<html>
<head>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.3.1/jquery.min.js">
</script>
<script>
$(document).ready(function(){
$("div").eq(1).css("background-color", "lightgreen");
});
</script>
</head>
<body>
<h1>How To Use Last Method for Traversing?</h1>
<p>This paragraph does not belong to any div element.</p>
<div style="border: 3px solid blue;">
<p>This is the first paragraph in this div.</p>
<p>This is the second paragraph in this div.</p>
</div>
<br>
<div style="border: 3px solid blue;">
<p>This is the first paragraph in this div.</p>
<p>This is the second paragraph in this div.</p>
</div>
<br>
<div style="border: 3px solid blue;">
<p>This is the first paragraph in this div.</p>
<p>This is the second paragraph in this div.</p>
</div>
</body>
</html>