SlideShare a Scribd company logo
jQuery
Why Study JavaScript?
JavaScript is one of the 3 languages all web developers must learn:
1. HTML to define the content of web pages
2. CSS to specify the layout of web pages
3. JavaScript to program the behavior of web pages
<html>
<body>
<h2>What Can JavaScript Do?</h2>
<p id="demo">JavaScript can change HTML content.</p>
<button type="button"
onclick='document.getElementById("demo").innerHTML = "Hello
JavaScript!"'>Click Me!</button>
</body>
</html>
Output:
What Can JavaScript Do?
JavaScript can change HTML content.
Click Me!
Display:
What Can JavaScript Do?
Hello JavaScript!
Click Me!
What is jQuery?
• jQuery is a fast, small, cross-platform and feature-rich JavaScript library. It is
designed to simplify the client-side scripting of HTML.
• It makes things like HTML document traversal and manipulation, animation,
event handling, and AJAX very simple with an easy-to-use API that works on a lot
of different type of browsers.
• The main purpose of jQuery is to provide an easy way to use JavaScript on your
website to make it more interactive and attractive. It is also used to add animation.
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
Why jQuery?
• 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 Install
• Downloading jQuery file from jQuery website.
• Referring to jQuery file through Content Delivery Networks.
Google CDN
<script src=http://ajax.googleapis.com/ajax/libs/jquery/1.11.0/jquery.min.js>
</script>
Microsoft CDN
<script src=“http://ajax.aspnetcdn.com/ajax/jQuery/jquery-1.11.0.min.js”>
</script>
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)
Examples:
$("p").hide() - hides all <p> elements.
The Document Ready Event
$(document).ready(function()
{
// jQuery methods go here...
});
$(function()
{
// jQuery methods go here...
});
#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:
<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:
This is a heading
This is a paragraph.
This is another paragraph.
Click me
Display:
This is a heading
This is a paragraph.
Click me
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="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 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>
Output:
This is a heading
This is a paragraph.
This is another paragraph.
Click me
Display:
This is another paragraph.
Click me
What are Events?
• All the different visitors' actions that a web page can respond to are called events.
• An event represents the precise moment when something happens.
Examples:
• moving a mouse over an element
• selecting a radio button
• clicking on an element
jQuery click() with Examples
• The click() is an inbuilt method in jQuery that starts the click event or attach a
function to run when a click event occurs.
Syntax:
• $(selector).click(function);
<html>
<head>
<script
src="https://ajax.googleapis.com/ajax/libs/jquery/3.3.1/jquery.min.js">
</script>
<!-- jQuery code to show the working of this method -->
<script>
$(document).ready(function() {
$("p").click();
});
</script>
<style>
p {
display: block;
width: 300px;
padding: 20px;
font-size: 30px;
border: 2px solid green;
}
</style>
</head>
<body>
<!-- click on this method -->
<p onclick="alert('tamilnadu was clicked')">This is tamilnadu state.</p>
</body>
</html>
Output:
Display:
<!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").click(function()
{
$(this).hide();
});
});
</script>
</head>
<body>
<p>If you click on me, tamilnadu will disappear.</p>
<p>Click me away!coimbatore</p>
<p>Click me too!erode</p>
</body>
</html>
jQuery
jQuery
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:
<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, tamilnadu will disappear.</p>
<p>Click me away!coimbatore</p>
<p>Click me too!erode</p>
</body>
</html>
Output:
Result:
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:
<html>
<head>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.5.1/jquery.min.js"></script>
<script>
$(document).ready(function(){
$("#p1").mouseenter(function(){
alert(“welcome tamilnadu");
});
});
</script>
</head>
<body>
<p id="p1">tamilnadu state</p>
</body>
</html>
Output:
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:
<html>
<head>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.5.1/jquery.min.js"></script>
<script>
$(document).ready(function(){
$("#p1").mouseleave(function(){
alert("Bye! You now leave tamilnadu!");
});
});
</script>
</head>
<body>
<p id="p1">This is tamilnadu state</p>
</body>
</html>
Output:
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:
<html>
<head>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.5.1/jquery.min.js"></script>
<script>
$(document).ready(function(){
$("#p1").mousedown(function(){
alert("Mouse down over tamilnadu!");
});
});
</script>
</head>
<body>
<p id="p1">This is tamilnadu state.</p>
</body>
</html>
Output:
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:
<!DOCTYPE html>
<html>
<head>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.5.1/jquery.min.js"></script>
<script>
$(document).ready(function(){
$("#p1").mouseup(function(){
alert("Mouse up over tamilnadu!");
});
});
</script>
</head>
<body>
<p id="p1">This is tamilnadu state.</p>
</body>
</html>
Output:
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="https://ajax.googleapis.com/ajax/libs/jquery/3.5.1/jquery.min.js"></script>
<script>
$(document).ready(function(){
$("#p1").hover(function(){
alert("You entered tamilnadu!");
},
function(){
alert("Bye! You now leave tamilnadu!");
});
});
</script>
</head>
<body>
<p id="p1">This is tamilnadu state.</p>
</body>
</html>
Output:
jQuery
focus()
• The focus() method attaches an event handler function to an HTML form field.
• The function is executed when the form field gets focus:
<!DOCTYPE html>
<html>
<head>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.5.1/jquery.min.js"></script>
<script>
$(document).ready(function()
{
$("input").focus(function()
{
$(this).css("background-color", "yellow");
});
$("input").blur(function()
{
$(this).css("background-color", "green");
});
});
</script>
</head>
<body>
Name: <input type="text" name="fullname"><br>
Email: <input type="text" name="email">
</body>
</html>
blur
• The jQuery blur event occurs when element loses focus. It can be generated by via
keyboard commands like tab key or mouse click anywhere on the page.
Output:
jQuery
Attach multiple event handlers to a <p>
element:
<!DOCTYPE html>
<html>
<head>
<script
src="https://ajax.googleapis.com/ajax/libs/jquery/3.5.1/jquery.mi
n.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 tamilnadu
state.</p>
</body>
</html>
Output:
jQuery Effects - Hide and Show
• We can hide and show HTML elements with the hide() and show() methods:
<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>tamilnadu will disappear.</p>
<button id="hide">Hide</button>
<button id="show">Show</button>
</body>
</html>
Output:
jQuery
jQuery Fading Methods
• jQuery we can fade an element in and out of visibility.
jQuery has the following fade methods:
• fadeIn()
• fadeOut()
• fadeToggle()
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.
<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(){
$("#div1").fadeIn();
$("#div2").fadeIn("slow");
$("#div3").fadeIn(3000);
});
});
</script>
</head>
<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>
Output:
jQuery fadeOut() Method
• The jQuery fadeOut() method is used to fade out a visible element.
Syntax:
$(selector).fadeOut(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.
Output:
jQuery 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.
Syntax:
$(selector).fadeToggle(speed,callback);
<!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(){
$("#div1").fadeToggle();
$("#div2").fadeToggle("slow");
$("#div3").fadeToggle(3000);
});
});
</script>
</head>
<body>
<p>Demonstrate fadeToggle() with different speed parameters.</p>
<button>Click to fade in/out boxes</button><br><br>
<div id="div1" style="width:80px;height:80px;background-color:red;"></div>
<br>
<div id="div2" style="width:80px;height:80px;background-color:green;"></div>
<br>
<div id="div3" style="width:80px;height:80px;background-color:blue;"></div>
</body>
</html>
Output:
jQuery
jQuery slideDown() Method
• The jQuery slideDown() method is used to slide down an element.
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:
<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: #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>
Output:
jQuery
jQuery slideUp() Method
• The jQuery slideUp() method is used to slide up an element.
Syntax:
$(selector).slideUp(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 slideUp() method:
<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").slideUp("slow");
});
});
</script>
<style>
#panel, #flip {
padding: 5px;
text-align: center;
background-color: #e5eecc;
border: solid 1px #c3c3c3;
}
#panel {
padding: 50px;
}
</style>
</head>
<body>
<div id="flip">Click to slide up panel</div>
<div id="panel">tamilnadu!</div>
</body>
</html>
Output:
jQuery
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.
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:
<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("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 the panel down or up</div>
<div id="panel">welcome tamilnadu CM!</div>
</body>
</html>
jQuery
jQuery
jQuery Effects - Animation
• jQuery Animations - The animate() Method
jQuery animate() method is used to create custom animations.
• Syntax:
$(selector).animate({params},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
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:
<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'});
});
});
</script>
</head>
<body>
<button>Start Animation</button>
<p>welcome my tamilnadu</p>
<div style="background:#98bf21;height:100px;width:100px;position:absolute;"></div>
</body>
</html>
position property:
• The position property specifies the type of positioning method used
for an element (static, relative, absolute, fixed, or sticky).
Output:
jQuery
<html>
<head>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.5.1/jquery.min.js"></script>
<style>
img{
position: relative; /* Required to move element */
}
</style>
<script>
$(document).ready(function(){
$("button").click(function(){
$("img").animate({left: 250});
});
});
</script>
</head>
<body>
<button>Start Animation</button>
<p>welcome to chennai</p>
<img src="vijay.jpg" alt="vijayimage" width=200 height=300></img>
</body>
</html>
Output:
Animation:

More Related Content

PPTX
lec 14-15 Jquery_All About J-query_.pptx
PPTX
J Query (Complete Course) by Muhammad Ehtisham Siddiqui
PPTX
Introduction to jQuery
PPTX
PDF
Advanced JQuery Mobile tutorial with Phonegap
PPTX
Unit3.pptx
PDF
PPT
Jquery PPT_Finalfgfgdfgdr5tryeujkhdwappt
lec 14-15 Jquery_All About J-query_.pptx
J Query (Complete Course) by Muhammad Ehtisham Siddiqui
Introduction to jQuery
Advanced JQuery Mobile tutorial with Phonegap
Unit3.pptx
Jquery PPT_Finalfgfgdfgdr5tryeujkhdwappt

Similar to jQuery (20)

PPTX
JQuery_and_Ajax.pptx
PPTX
JAVASCRIPT 1.pptx.pptx
PPTX
Jquery Complete Presentation along with Javascript Basics
PPTX
Introduction to jquery mobile with Phonegap
PPTX
jQuery for web development
PDF
Short intro to JQuery and Modernizr
PDF
Learning jQuery made exciting in an interactive session by one of our team me...
PPTX
jQuery PPT
PPTX
JavaScriptL18 [Autosaved].pptx
PPTX
Wt unit 5
PPTX
jQuery basics
PPT
jQuery introduction/basic concept /welcome jQuery
PPTX
jQuery - Web Engineering
PPTX
unit4 wp.pptxjvlbpuvghuigv8ytg2ugvugvuygv
PPTX
Client Web
PPT
J query presentation
JQuery_and_Ajax.pptx
JAVASCRIPT 1.pptx.pptx
Jquery Complete Presentation along with Javascript Basics
Introduction to jquery mobile with Phonegap
jQuery for web development
Short intro to JQuery and Modernizr
Learning jQuery made exciting in an interactive session by one of our team me...
jQuery PPT
JavaScriptL18 [Autosaved].pptx
Wt unit 5
jQuery basics
jQuery introduction/basic concept /welcome jQuery
jQuery - Web Engineering
unit4 wp.pptxjvlbpuvghuigv8ytg2ugvugvuygv
Client Web
J query presentation
Ad

Recently uploaded (20)

PDF
Microbial disease of the cardiovascular and lymphatic systems
PDF
Physiotherapy_for_Respiratory_and_Cardiac_Problems WEBBER.pdf
PDF
Abdominal Access Techniques with Prof. Dr. R K Mishra
PDF
FourierSeries-QuestionsWithAnswers(Part-A).pdf
PDF
Pre independence Education in Inndia.pdf
PDF
Supply Chain Operations Speaking Notes -ICLT Program
PPTX
IMMUNITY IMMUNITY refers to protection against infection, and the immune syst...
PPTX
PPH.pptx obstetrics and gynecology in nursing
PDF
Insiders guide to clinical Medicine.pdf
PDF
3rd Neelam Sanjeevareddy Memorial Lecture.pdf
PDF
O5-L3 Freight Transport Ops (International) V1.pdf
PDF
Origin of periodic table-Mendeleev’s Periodic-Modern Periodic table
PPTX
Pharmacology of Heart Failure /Pharmacotherapy of CHF
PDF
Chapter 2 Heredity, Prenatal Development, and Birth.pdf
PPTX
Institutional Correction lecture only . . .
PPTX
Cell Types and Its function , kingdom of life
PPTX
Microbial diseases, their pathogenesis and prophylaxis
PDF
Basic Mud Logging Guide for educational purpose
PDF
Anesthesia in Laparoscopic Surgery in India
PDF
Mark Klimek Lecture Notes_240423 revision books _173037.pdf
Microbial disease of the cardiovascular and lymphatic systems
Physiotherapy_for_Respiratory_and_Cardiac_Problems WEBBER.pdf
Abdominal Access Techniques with Prof. Dr. R K Mishra
FourierSeries-QuestionsWithAnswers(Part-A).pdf
Pre independence Education in Inndia.pdf
Supply Chain Operations Speaking Notes -ICLT Program
IMMUNITY IMMUNITY refers to protection against infection, and the immune syst...
PPH.pptx obstetrics and gynecology in nursing
Insiders guide to clinical Medicine.pdf
3rd Neelam Sanjeevareddy Memorial Lecture.pdf
O5-L3 Freight Transport Ops (International) V1.pdf
Origin of periodic table-Mendeleev’s Periodic-Modern Periodic table
Pharmacology of Heart Failure /Pharmacotherapy of CHF
Chapter 2 Heredity, Prenatal Development, and Birth.pdf
Institutional Correction lecture only . . .
Cell Types and Its function , kingdom of life
Microbial diseases, their pathogenesis and prophylaxis
Basic Mud Logging Guide for educational purpose
Anesthesia in Laparoscopic Surgery in India
Mark Klimek Lecture Notes_240423 revision books _173037.pdf
Ad

jQuery