10.introduction To JQuery
10.introduction To JQuery
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.
• HTML/DOM manipulation
• CSS manipulation
• HTML event methods
• Effects and animations
• AJAX
• Utilities
Why jQuery?
o There are lots of other JavaScript libraries out there, but jQuery is
probably the most popular, and also the most extendable. It is very fast
and extensible.
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)
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.6.4.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 CDN:
<head>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.6.4/jquery.min
.js"></script>
</head>
jQuery Syntax
With jQuery you select (query) HTML elements and perform "actions" on
them.
The jQuery syntax is tailor-made for selecting HTML elements and performing
some action on the element(s).
Examples:
$(document).ready(function(){
});
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:
• The jQuery team has also created an even shorter method for the
document ready event:
• $(function(){
});
jQuery Selectors
jQuery selectors are one of the most important parts of the jQuery library.
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: $().
$("p")
Example
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
$(document).ready(function(){
$("button").click(function(){
$("#test").hide();
});
});
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:
Example
$(document).ready(function(){
$("button").click(function(){
$(".test").hide();
});
});
$("ul li:first") Selects the first <li> element of the first <ul>
$("a[target!='_blank']") Selects all <a> elements with a target attribute value NOT equal
to "_blank"
Examples:
$("p").click();
The next step is to define what should happen when the event fires. You must
pass a function to the event:
$("p").click(function(){
// action goes here!!
Commonly Used jQuery Event Methods
$(document).ready()
click()
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();
});
dblclick()
The function is executed when the user double-clicks on the HTML element:
Example
$("p").dblclick(function(){
$(this).hide();
});
mouseenter()
The function is executed when the mouse pointer enters the HTML element:
Example
$("#p1").mouseenter(function(){
alert("You entered p1!");
});
mouseleave()
The function is executed when the mouse pointer leaves the HTML element:
Example
$("#p1").mouseleave(function(){
alert("Bye! You now leave p1!");
});
mousedown()
The function is executed, when the left, middle or right mouse button is pressed
down, while the mouse is over the HTML element:
Example
$("#p1").mousedown(function(){
alert("Mouse down over p1!");
});
mouseup()
The function is executed, when the left, middle or right mouse button is
released, while the mouse is over the HTML element:
Example
$("#p1").mouseup(function(){
alert("Mouse up over p1!");
});
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:
Example
$("#p1").hover(function(){
alert("You entered p1!");
},
function(){
alert("Bye! You now leave p1!");
});
focus()
The focus() method attaches an event handler function to an HTML form field.
Example
$("input").focus(function(){
$(this).css("background-color", "#cccccc");
});
blur()
The blur() method attaches an event handler function to an HTML form field.
Example
$("input").blur(function(){
$(this).css("background-color", "#ffffff");
});
Example
$("p").on("click", function(){
$(this).hide();
});
Example
$("p").on({
mouseenter: function(){
$(this).css("background-color", "lightgray");
},
mouseleave: function(){
$(this).css("background-color", "lightblue");
},
click: function(){
$(this).css("background-color", "yellow");
}
});
jQuery Effects
jQuery enables us to add effects on a web page. jQuery effects can be categorized into
fading, sliding, hiding/showing and animation effects.
jQuery provides many methods for effects on a web page. A complete list of jQuery
effect methods are given below:
2 clearQueue() It is used to remove all remaining queued functions from the selected
elements.
3) delay() sets delay execution for all the queued functions on the selected
elements.
4 dequeue() It is used to remove the next function from the queue, and then execute
the function.
7) fadeto() adjusts opacity for the matched element. In other words, it fades in/out
the selected elements.
8) fadetoggle() shows or hides the matched element. In other words, toggles between
the fadeIn() and fadeOut() methods.
9) finish() It stops, removes and complete all queued animation for the selected
elements.
11) queue() shows or manipulates the queue of methods i.e. to be executed on the
selected elements.
16) stop() stops the animation which is running on the matched elements.
17) toggle() shows or hides the matched elements. In other words, it toggles between
the hide() and show() methods.
jQuery hide()
The jQuery hide() method is used to hide the selected elements.
Syntax:
1. $(selector).hide();
2. $(selector).hide(speed, callback);
3. $(selector).hide(speed, easing, callback);
speed: It is an optional parameter. It specifies the speed of the delay. Its possible vales
are slow, fast and milliseconds.
jQuery show()
The jQuery show() method is used to show the selected elements.
Syntax:
1. $(selector).show();
2. $(selector).show(speed, callback);
3. $(selector).show(speed, easing, callback);
jQuery toggle()
The jQuery toggle() is a special type of method which is used to toggle between the
hide() and show() method. It shows the hidden elements and hides the shown element.
Syntax:
1. $(selector).toggle();
2. $(selector).toggle(speed, callback);
3. $(selector).toggle(speed, easing, callback);
4. $(selector).toggle(display);
jQuery fadeIn()
jQuery fadeIn() method is used to fade in the element.
Syntax:
1. $(selector).fadein();
2. $(selector).fadeIn(speed,callback);
3. $(selector).fadeIn(speed, easing, callback);
Query fadeOut()
The jQuery fadeOut() method is used to fade out the element.
Syntax:
1. $(selector).fadeOut();
2. $(selector).fadeOut(speed,callback);
3. $(selector).fadeOut(speed, easing, callback);
jQuery fadeToggle()
jQuery fadeToggle() method is used to toggle between the fadeIn() and fadeOut()
methods. If the elements are faded in, it will make them faded out and if they are faded
out it will make them faded in.
Syntax:
1. $(selector).fadeToggle();
2. $(selector).fadeToggle(speed,callback);
3. $(selector).fadeToggle(speed, easing, callback);
jQuery fadeTo()
jQuery fadeTo() method is used to fading to a given opacity.
Syntax:
1. $(selector).fadeTo(speed, opacity);
2. $(selector).fadeTo(speed, opacity, callback);
3. $(selector).fadeTo(speed, opacity, easing, callback);
opacity:It specifies the opacity. The opacity value ranges between 0 and 1.
jQuery slideDown()
jQuery slideDown() method is used to slide down an element.
Syntax:
1. $(selector).slideDown(speed);
2. $(selector).slideDown(speed, callback);
3. $(selector).slideDown(speed, easing, callback);
jQuery slideUp()
jQuery slideDown() method is used to slide up an element.
Syntax:
1. $(selector).slideUp(speed);
2. $(selector).slideUp(speed, callback);
3. $(selector).slideUp(speed, easing, callback);
jQuery animate()
The jQuery animate() method provides you a way to create custom animations.
Syntax:
The speed parameter is optional and specifies the duration of the effect. It can be set as
"slow" , "fast" or milliseconds.
jQuery delay()
The jQuery delay() method is used to delay the execution of functions in the queue. It is
a best method to make a delay between the queued jQuery effects. The jQUery delay ()
method sets a timer to delay the execution of the next item in the queue.
Syntax:
speed: It is an optional parameter. It specifies the speed of the delay. Its possible vales
are slow, fast and milliseconds.
queueName: It is also an optional parameter. It specifies the name of the queue. Its
default value is "fx" the standard queue effect.