UNIT-3
UNIT-3
INTRODUCTION TO
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.
• 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
ADDING JQUERY TO YOUR WEB PAGES
});
EXAMPLE OF JQUERY
<head>
<script src=“jquery.js”></script>
</head>
<body>
<script>
$(document).ready(function(){
alert(1);
});
</script>
</body>
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: $().
Type of Selector :-
Document.getElementByClassName(“classname”)
$(“.classname”)
Document.getElementByTagName(“tagname”)
$(“tagname”)
Document.getElementById(“idname”)
$(“#idname”)
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:
$(document).ready(function(){
$("button").click(function(){
$("p").hide();
});
});
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:
Example
<script>
$(document).ready(function(){
$("button").click(function(){
$("#test").hide();
});
});
</script>
<p>This is a paragraph.</p>
<p id="test">This is another paragraph.</p>
THE .CLASS SELECTOR
Mouse Events :
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:
Example
$("p").click(function(){
$(this).hide();
});
JQUERY EVENTS CON…
Mouse Events :
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:
Example
$("p").dblclick(function(){
$(this).hide();
});
JQUERY EVENTS CON…
Mouse Events :
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:
Example
$("#p1").mouseenter(function(){
alert("You entered p1!");
});
JQUERY EVENTS CON…
Mouse Events :
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:
Example
$("#p1").mouseleave(function(){
alert("Bye! You now leave p1!");
});
JQUERY EVENTS CON…
Form Events :
1) focus()
The focus() method attaches an event handler function to an HTML form field.
The function is executed when the form field gets focus:
Example
$("input").focus(function(){
$(this).css("background-color", "#cccccc");
});
JQUERY EVENTS CON…
Form Events :
2) blur()
The blur() method attaches an event handler function to an HTML form field.
The function is executed when the form field loses focus:
Example
$("input").blur(function(){
$(this).css("background-color", "#ffffff");
});
JQUERY EVENTS CON…
Form Events :
$(document).ready(function(){
$("input").focus(function(){
$(this).css("background-color", "yellow");
});
$("input").blur(function(){
$(this).css("background-color", "green");
});
});
JQUERY EVENTS CON…
key Events :
keydown - The key is on its way down
keypress - The key is pressed down
keyup - The key is released
The keypress() method triggers the keypress event, or attaches a function to run when a keypress event
occurs.
The keypress event is similar to the keydown event. The event occurs when a button is pressed down.
However, the keypress event is not fired for all keys (e.g. ALT, CTRL, SHIFT, ESC). Use the keydown() method
to also check these keys.
JQUERY EVENTS CON…
$("span").text(i += 1);
<!DOCTYPE html>
});
<html>
});
<head>
</script>
<script
src="https://ajax.googleapis.com/ajax/l </head>
ibs/jquery/3.5.1/jquery.min.js"></script <body>
>
Enter your name: <input type="text">
<script>
<p>Keypresses: <span>0</span></p>
i = 0;
</body>
$(document).ready(function(){
</html>
$("input").keyup(function(){
JQUERY EVENTS CON…
Document/Windows Events :
Resize() - An event handler function is attached to an window element by the resize() method and that
function is executed whenever the size of browser window changes.
Scroll() - The scroll event of jQuery occurs when the user scrolls in the specified element and then the
scroll() method triggers the scroll event, or attaches a function to run.
JQUERY EVENTS CON…
<!DOCTYPE html>
<html> });
<head> });
</script>
<script
src="https://ajax.googleapis.com/ajax/libs/j </head>
query/3.2.1/jquery.min.js"></script>
<body>
<script> <h2>Heading 1</h2>
x = 0; <p>This is the first paragraph.<span>0</span> times.</p>
$(document).ready(function(){ <p>Window resized <span>0</span> times.</p>
$(window).resize(function(){ <p>Try resizing your browser window.</p>
$("span").text(x += 1); </body>
</html>
JQUERY EFFECTS
Effects :-
1. Hide
2. Show
3. Fade
4. Slide
5. Animation
6. Stop
7. Callback and function
8. Chaining
JQUERY EFFECTS CON…
Hide () Show ()
With jQuery, you can hide HTML elements
With jQuery, you can show HTML elements
with the hide() and show() methods:
with the show() methods:
$("#hide").click(function(){
$("p").hide(); $("#show").click(function(){
}); $("p").show();
});
JQUERY EFFECTS CON…
Slid () :- jQuery has the following slide methods: 1) slideDown() 2) slideUp() 3) slideToggle()
1) slideeDown :- 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:
Example
$("#flip").click(function(){
$("#panel").slideDown();
});
JQUERY EFFECTS CON…
Stop () :- The jQuery stop() method is used to stop an animation or effect before it is finished.
The stop() method works for all jQuery effect functions, including sliding, fading and custom animations.
Syntax:
$(selector).stop(stopAll,goToEnd);
The optional stopAll parameter specifies whether also the animation queue should be cleared or not. Default is false, which means that
only the active animation will be stopped, allowing any queued animations to be performed afterwards.
The optional goToEnd parameter specifies whether or not to complete the current animation immediately. Default is false.
So, by default, the stop() method kills the current animation being performed on the selected element.
The following example demonstrates the stop() method, with no parameters:
Example
$("#stop").click(function(){
$("#panel").stop();
});
JQUERY EFFECTS CON…
Callback :-JavaScript statements are executed line by line. However, with effects, the next line of code can be run
even though the effect is not finished. This can create errors.
To prevent this, you can create a callback function.
A callback function is executed after the current effect is finished.
Typical syntax: $(selector).hide(speed,callback);
Examples
The example below has a callback parameter that is a function that will be executed after the hide effect is completed:
Example with Callback Example without Callback
$("button").click(function(){ $("button").click(function(){
$("p").hide("slow", function(){ $("p").hide(1000);
alert("The paragraph is now hidden");
alert("The paragraph is now hidden");
});
});
});
JQUERY EFFECTS CON…
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).
Tip: This way, browsers do not have to find the same element(s) more than once.
To chain an action, you simply append the action to the previous action.
The following example chains together the css(), slideUp(), and slideDown() methods. The "p1" element first changes to red, then it
slides up, and then it slides down:
Example
$(document).ready(function(){
$("button").click(function(){
$("#p1").css("color", "red").slideUp(2000).slideDown(2000);
});
});
JQUERY HTML
jQuery contains powerful methods for changing and manipulating HTML elements and attributes.
1. Jquery Get
2. Jquery Set
3. Jquery Add
4. Jquery Remove
JQUERY HTML
Jquery Get :-
1. Text()
2. Html()
3. Attr()
4. Val()
html() - Sets or returns the content of selected elements (including HTML markup)
Example :
Example :
3) val : $(document).ready(function()
{
4) Attr :
$("#val").click(function(){ $(document).ready(function()
var name=$("#nm").val(); {
var clas=$("#cls").val(); $(“#attr").click(function(){
document.write("Name is :- "-name- alert($("#w3s").attr("href"));
"<br>"+"Class Name is :- "+clas); });
}); });
});
JQUERY HTML
Jquery Set :-
We will use the same three methods from the previous page to set content:
The following example demonstrates how to set content with the jQuery text(), html(), and val()
methods:
JQUERY HTML
Example :
<body>
<script>
$(document).ready(function(){
<p id="test1">This is a paragraph.</p>
$("#btn1").click(function(){
<p id="test2">This is another paragraph.</p>
$("#test1").text("Hello world!");
});
<p>Input field: <input type="text" id="test3"
$("#btn2").click(function(){
value="Mickey Mouse"></p>
$("#test2").html("<b>Hello world!</b>");
});
<button id="btn1">Set Text</button>
$("#btn3").click(function(){
<button id="btn2">Set HTML</button>
$("#test3").val("Dolly Duck");
<button id="btn3">Set Value</button>
});
});
</script> </body>
JQUERY HTML
Jquery Set :-
The following example demonstrates how to change (set) the value of the href attribute in a link:
Example
$("button").click(function(){
$("#w3s").attr("href", "https://www.w3schools.com/jquery/");
});
JQUERY HTML
Jquery Add :-
We will look at four jQuery methods that are used to add new content:
$(document).ready(function(){ <ol>
$("#btn1").click(function(){ <li>List item 1</li>
$("p").prepend("<b>Prepended text</b>. "); <li>List item 2</li>
}); <li>List item 3</li>
$("#btn2").click(function(){
</ol>
$("ol").prepend("<li>Prepended item</li>");
<button id="btn1">Prepend text</button>
});
<button id="btn2">Prepend list item</button>
});
</body>
</script>
JQUERY HTML
<p>This is a paragraph.</p>
</script>
<p>This is another paragraph.</p>
css("propertyname");
The following example will return the background-color value of the FIRST matched element:
JQUERY MANIPULATING CSS
<script>
$(document).ready(function(){
$("button").click(function(){
<p style="background-color:#00ff00">This is a
$("p").css("background-color", "yellow");
paragraph.</p>
});
<p style="background-color:#0000ff">This is a
}); paragraph.</p>
</script> <p>This is a paragraph.</p>
<body> <button>Set background-color of p</button>
<h2>This is a heading</h2> </body>
<p style="background-color:#ff0000">This is a paragraph.</p>
JQUERY MANIPULATING CSS
Set Multiple CSS Properties
To set multiple CSS properties, use the following syntax:
css({"propertyname":"value","propertyname":"value",...});
$("p").css({"background-color": "yellow", "font-size": "200%"});
jQuery - Dimensions
jQuery Dimension Methods
jQuery has several important methods for working with dimensions:
1) width() 2) height()
3) innerWidth() 4) innerHeight()
5) outerWidth() 6) outerHeight()
JQUERY MANIPULATING CSS
jQuery width() and height() Methods
The width() method sets or returns the width of an element (excludes padding, border and margin).
The height() method sets or returns the height of an element (excludes padding, border and margin).
The following example returns the width and height of a specified <div> element:
<script>
$(document).ready(function(){
$("button").click(function(){
var txt = "";
txt += "Width of div: " + $("#div1").width() + "</br>";
txt += "Height of div: " + $("#div1").height();
$("#div1").html(txt);
});
JQUERY MANIPULATING CSS
jQuery innerWidth() and innerHeight() Methods
The innerWidth() method returns the width of an element (includes padding).
The innerHeight() method returns the height of an element (includes padding).
The following example returns the inner-width/height of a specified <div> element:
Example
$("button").click(function(){
var txt = "";
txt += "Inner width: " + $("#div1").innerWidth() + "</br>";
txt += "Inner height: " + $("#div1").innerHeight();
$("#div1").html(txt); });
JQUERY MANIPULATING CSS
jQuery innerWidth() and innerHeight() Methods
The innerWidth() method returns the width of an element (includes padding).
The innerHeight() method returns the height of an element (includes padding).
The following example returns the inner-width/height of a specified <div> element:
Example
$("button").click(function(){
var txt = "";
txt += "Inner width: " + $("#div1").innerWidth() + "</br>";
txt += "Inner height: " + $("#div1").innerHeight();
$("#div1").html(txt); });
JQUERY MANIPULATING CSS
jQuery outerWidth() and outerHeight() Methods
The outerWidth() method returns the width of an element (includes padding and border).
The outerHeight() method returns the height of an element (includes padding and border).
The following example returns the outer-width/height of a specified <div> element:
Example
$("button").click(function(){
var txt = "";
txt += "Outer width: " + $("#div1").outerWidth() + "</br>";
txt += "Outer height: " + $("#div1").outerHeight();
$("#div1").html(txt); });
JQUERY MANIPULATING CSS
The outerWidth(true) method returns the width of an element (includes padding, border, and margin).
The outerHeight(true) method returns the height of an element (includes padding, border, and margin).
Example
$("button").click(function(){
var txt = "";
txt += "Outer width (+margin): " + $("#div1").outerWidth(true) + "</br>";
txt += "Outer height (+margin): " + $("#div1").outerHeight(true);
$("#div1").html(txt);
});
JQUERY TRAVERSING
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()