CHAPTER - 3 - LECTURE NOTES - ON - Jquery
CHAPTER - 3 - LECTURE NOTES - ON - Jquery
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.
Many of the biggest companies on the Web use jQuery, such as:
❖ Google
❖ Microsoft
❖ IBM
❖ Netflix
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 jQuery.com.
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="jquery-3.5.1.min.js"></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="https://ajax.googleapis.com/ajax/libs/jquery/3.5.1/jquery.mi
n.js"></script>
</head>
jQuery Syntax
The jQuery syntax is tailor-made for selecting HTML elements and
performing some action on the element(s).
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".
});
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:
● Trying to hide an element that is not created yet
● Trying to get the size of an image that is not loaded yet
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: $().
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="https://ajax.googleapis.com/ajax/libs/jquery/3.5.1/jquery.min.js">
</script>
<script>
$(document).ready(function(){
$("button").click(function(){
$("p").hide();
});
});
</script>
</head>
<body>
<h2>This is a heading</h2>
<p>This is a paragraph.</p>
<p>This is another paragraph.</p>
$("#test")
Example
When a user clicks on a button, the element with id="test" will be hidden:
<html>
<head>
<script
src="https://ajax.googleapis.com/ajax/libs/jquery/3.5.1/jquery.min.js"><
/script>
<script>
$(document).ready(function(){
$("button").click(function(){
$("#test").hide();
});
});
</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>
OUTPUT:
Examples of jQuery Selectors
Syntax Description
Example:
<html>
<head>
<script
src="https://ajax.googleapis.com/ajax/libs/jquery/3.5.1/jquery.min.js">
</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>
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:
Example:
<html>
<head>
<script
src="https://ajax.googleapis.com/ajax/libs/jquery/3.5.1/jquery.min.js"><
/script>
<script>
$(document).ready(function(){
$("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>
The on() Method
The on() method attaches one or more event handlers for the selected
elements.
Attach a click event to a <p> element:
Example:
<html>
<head>
<script
src="https://ajax.googleapis.com/ajax/libs/jquery/3.5.1/jquery.min.js">
</script>
<script>
$(document).ready(function(){
$("p").on({
mouseenter: function(){
$(this).css("background-color", "lightgray");
},
mouseleave: function(){
$(this).css("background-color", "lightblue");
},
click: function(){
$(this).css("background-color", "yellow");
}
});
});
</script>
</head>
<body>
<p>Click or move the mouse pointer over this paragraph.</p>
</body>
</html>
Example:
<html>
<head>
<script
src="https://ajax.googleapis.com/ajax/libs/jquery/3.5.1/jquery.min.js"><
/script>
<script>
$(document).ready(function(){
$("#hide").click(function(){
$("p").hide();
});
$("#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>
OUTPUT:
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.
The following example demonstrates the speed parameter with hide():
Example:
<html>
<head>
<script
src="https://ajax.googleapis.com/ajax/libs/jquery/3.5.1/jquery.min.js"><
/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>
</body>
</html>
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:
Example:
<html>
<head>
<script
src="https://ajax.googleapis.com/ajax/libs/jquery/3.5.1/jquery.min.js"><
/script>
<script>
$(document).ready(function(){
$("button").click(function(){
$("p").toggle(1000);
});
});
</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>
jQuery Effects - Sliding
Syntax:
$(selector).slideDown(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
sliding completes.
The following example demonstrates the slideDown() method:
Example:
<html>
<head>
<script
src="https://ajax.googleapis.com/ajax/libs/jquery/3.5.1/jquery.min.js"><
/script>
<script>
$(document).ready(function(){
$("#flip").click(function(){
$("#panel").slideDown("slow");
});
});
</script>
<style>
#panel, #flip {
padding: 5px;
text-align: center;
background-color: green;
border: solid 1px pink;
}
#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>
OUTPUT:
#panel {
padding: 50px;
}
</style>
</head>
<body>
<div id="flip">Click to slide up panel</div>
<div id="panel">Hello world!</div>
</body>
</html>
OUTPUT:
Syntax:
$(selector).slideToggle(speed,callback);
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:
<html>
<head>
<script
src="https://ajax.googleapis.com/ajax/libs/jquery/3.5.1/jquery.min.js"><
/script>
<script>
$(document).ready(function(){
$("#flip").click(function(){
$("#panel").slideToggle(2500);
});
});
</script>
<style>
#panel, #flip {
padding: 10px;
text-align: center;
background-color: lightgray;
border: solid 1px red;
}
#panel {
padding: 50px;
display: none;
}
</style>
</head>
<body>
<div id="flip">Click to slide the panel down or up</div>
<div id="panel">Hello world!</div>
</body>
</html>
<script>
$(document).ready(function(){
$("button").click(function(){
$("div").animate({left: '250px'});
});
});
</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>
OUTPUT:
jQuery animate() - Manipulate Multiple Properties
Notice that multiple properties can be animated at the same time:
Example:
<html>
<head>
<script
src="https://ajax.googleapis.com/ajax/libs/jquery/3.5.1/jquery.min.js"><
/script>
<script>
$(document).ready(function(){
$("button").click(function(){
$("div").animate({
left: '250px',
opacity: '0.5',
height: '150px',
width: '150px'
});
});
});
</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:100px;position:absolute;"
></div>
</body>
</html>
OUTPUT:
jQuery Method Chaining
Until now we have been writing jQuery statements one at a time (one after
the other).
However, there is a technique called chaining, that allows us to run
multiple jQuery commands, one after the other, on the same element(s).
To chain an action, you simply append the action to the previous action.
and slideDown() methods. The "p1" element first changes to red, then it
slides up, and then it slides down:
Example:
<html>
<head>
<script
src="https://ajax.googleapis.com/ajax/libs/jquery/3.5.1/jquery.min.js"><
/script>
<script>
$(document).ready(function(){
$("button").click(function(){
$("#p1").css("color", "red").slideUp(100).slideDown(2000);
});
});
</script>
</head>
<body>
<p id="p1">jQuery is fun!!</p>
<button>Click me</button>
</body>
</html>
jQuery bind()
The jQuery bind() event is used to attach one or more event handlers for
selected elements from a set of elements. It specifies a function to run when
the event occurs.
It is generally used together with other events of jQuery.
Syntax:
$(selector).bind(event,data,function,map)
<!DOCTYPE html>
<html>
<head>
<script
src="https://ajax.googleapis.com/ajax/libs/jquery/3.5.1/jquery.min.js"><
/script>
<script>
$(document).ready(function(){
$("p").bind("click", function(){
alert("This paragraph was clicked.");
});
});
</script>
</head>
<body>
<p>Click on the statement.</p>
</body>
</html>
Example1
In this example, there are two h3 heading elements and a button. On
hovering the elements with text Hover me, the mouseover event will be
attached to them.
Here, we are using the unbind() function to unbind the mouseover event.
We can check the working of the unbind() method by first hovering
an h3 element and then clicking the given button. On hovering, the style of
h3 heading will get changed, and when we click the given button, the
hovering will not affect another h3 element.
<!DOCTYPE html>
<html>
<head>
<script src =
"https://ajax.googleapis.com/ajax/libs/jquery/3.5.1/jquery.min.js">
</script>
<script>
$(document).ready(function(){
$("h3").bind("mouseover",function(){
$(this).css({"background-color": "pink", "font-size":
"25px"}).text("Hovered");
});
$("#b3").click(function(){
$("h3").unbind("mouseover");
});
});
</script>
</head>
<body>
<h2> It is an example of using the jQuery unbind() method. </h2>
<p> Move the cursor over the below heading to attach the mouseover
event. </p>
<h3> Hover me </h3>
<button id = "b3"> Remove event </button>
<h3> Hover me </h3>
</body>
</html>
<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>
<h1>Welcome to My Homepage</h1>
<p>This is a paragraph.</p>
</body>
</html>
OUTPUT:
<script>
$(document).ready(function(){
$("p").eq(2).css("background-color", "yellow");
});
</script>
</head>
<body>
<h1>Welcome to My Homepage</h1>
OUTPUT:
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="https://ajax.googleapis.com/ajax/libs/jquery/3.5.1/jquery.min.js"><
/script>
<script>
$(document).ready(function(){
$("p").filter(".intro").css("background-color", "yellow");
});
</script>
</head>
<body>
<h1>Welcome to My Homepage</h1>
</body>
</html>
OUTPUT:
<script>
$(document).ready(function(){
$("p").not(".intro").css("background-color", "yellow");
});
</script>
</head>
<body>
<h1>Welcome to My Homepage</h1>
<script>
$(document).ready(function() {
$("#basic-form").validate({
rules: {
name : {
required: true,
minlength: 3
},
age: {
required: true,
number: true,
},
email: {
required: true,
email: true
},
}
});
});
</script>
</head>
<body>