J Query
J Query
jQuery tutorial for beginners and professionals provides deep knowledge of jQuery
technology. Our jQuery tutorial will help you to learn jQuery fundamentals, example,
selectors, events, effects, traversing, CSS and attributes.
What is jQuery
o jQuery is a small and lightweight JavaScript library.
o jQuery is cross-platform.
o jQuery means "write less do more".
o jQuery simplifies AJAX call and DOM manipulation.
jQuery Example
In this tutorial, you will get a lot of jQuery examples to understand the topic well.
Let's see a simple jQuery example.
File: firstjquery.html
1. <!DOCTYPE html>
2. <html>
3. <head>
4. <title>First jQuery Example</title>
5. <script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/
jquery/2.1.3/jquery.min.js">
6. </script>
7. <script type="text/javascript" language="javascript">
8. $(document).ready(function() {
9. $("p").css("background-color", "pink");
10. });
11. </script>
12. </head>
13. <body>
14. <p>This is first paragraph.</p>
15. <p>This is second paragraph.</p>
16. <p>This is third paragraph.</p>
17. </body>
18. </html>
Test it Now
Output:
Play Video
Prerequisite
Before learning jQuery, you must have the basic knowledge of JavaScript.
Audience
Our jQuery tutorial is designed to help beginners and professionals.
Problem
We assure that you will not find any problem in this jQuery tutorial. But if there is any
mistake, please post the problem in contact form.
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.
What is jQuery
jQuery is a small, light-weight and fast JavaScript library. It is cross-platform and
supports different types of browsers. It is also referred as ?write less do more?
because it takes a lot of common tasks that requires many lines of JavaScript code to
accomplish, and binds them into methods that can be called with a single line of
code whenever needed. It is also very useful to simplify a lot of the complicated
things from JavaScript, like AJAX calls and DOM manipulation.
jQuery Features
Following are the important features of jQuery.
Pause
Unmute
Current Time 0:01
Duration 18:10
Loaded: 2.94%
Â
Fullscreen
o HTML manipulation
o DOM manipulation
o DOM element selection
o CSS manipulation
o Effects and Animations
o Utilities
o AJAX
o HTML event methods
o JSON Parsing
o Extensibility through plug-ins
So, you can say that out of the lot of JavaScript frameworks, jQuery is the most
popular and the most extendable. Many of the biggest companies on the web use
jQuery.
o Microsoft
o Google
o IBM
o Netflix
jQuery History
jQuery was first released in January 2006 by John Resig at BarCamp NYC. It is
currently headed by Timmy Wilson and maintained by a team of developers.
Nowadays, jQuery is widely used technology. Most of the websites are using jQuery.
1.0 26,August,2006
1.1 14,January,2007
jQuery Example
jQuery is developed by Google. To create the first jQuery example, you need to use
JavaScript file for jQuery. You can download the jQuery file from jquery.com or use
the absolute URL of jQuery file.
In this jQuery example, we are using the absolute URL of jQuery file. The jQuery
example is written inside the script tag.
File: firstjquery.html
1. <!DOCTYPE html>
2. <html>
3. <head>
4. <title>First jQuery Example</title>
5. <script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/
jquery/2.1.3/jquery.min.js">
6. </script>
7. <script type="text/javascript" language="javascript">
8. $(document).ready(function() {
9. $("p").css("background-color", "cyan");
10. });
11. </script>
12. </head>
13. <body>
14. <p>The first paragraph is selected.</p>
15. <p>The second paragraph is selected.</p>
16. <p>The third paragraph is selected.</p>
17. </body>
18. </html>
Test it Now
Output:
1. $(document).ready(function() {
2. $("p").css("color", "red");
3. });
1. $(function() {
2. $("p").css("color", "red");
3. });
Let's see the full example of jQuery using shorthand notation $().
File: shortjquery.html
1. <!DOCTYPE html>
2. <html>
3. <head>
4. <title>Second jQuery Example</title>
5. <script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/
jquery/2.1.3/jquery.min.js">
6. </script>
7. <script type="text/javascript" language="javascript">
8. $(function() {
9. $("p").css("color", "red");
10. });
11. </script>
12. </head>
13. <body>
14. <p>The first paragraph is selected.</p>
15. <p>The second paragraph is selected.</p>
16. <p>The third paragraph is selected.</p>
17. </body>
18. </html>
Test it Now
Output:
← Prev
jQuery Selectors
jQuery Selectors are used to select and manipulate HTML elements. They are very
important part of jQuery library.
With jQuery selectors, you can find or select HTML elements based on their id,
classes, attributes, types and much more from a DOM.
In simple words, you can say that selectors are used to select one or more HTML
elements using jQuery and once the element is selected then you can perform
various operation on that.
All jQuery selectors start with a dollor sign and parenthesis e.g. $(). It is known as the
factory function.
Pause
Unmute
Current Time 0:01
Duration 18:10
Loaded: 3.30%
Â
Fullscreen
Let's take a simple example to see the use of Tag selector. This would select all the
elements with a tag name
File: firstjquery.html
1. <!DOCTYPE html>
2. <html>
3. <head>
4. <title>First jQuery Example</title>
5. <script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/
jquery/2.1.3/jquery.min.js">
6. </script>
7. <script type="text/javascript" language="javascript">
8. $(document).ready(function() {
9. $("p").css("background-color", "pink");
10. });
11. </script>
12. </head>
13. <body>
14. <p>This is first paragraph.</p>
15. <p>This is second paragraph.</p>
16. <p>This is third paragraph.</p>
17. </body>
18. </html>
Test it Now
Output:
Note: 1. All of the above discussed selectors can be used alone or with the
combination of other selectors.
Note: 2. If you have any confliction with theuse of dollor sign $ in any JavaScript
library then you can use jQuery() function instead of factory function $(). The
factory function $() and the jQuery function is the same.
2) #ID: It selects a single element that matches with the given id.
3) .Class: It selects all elements that matches with the given class.
5) Multiple Elements A,B,C It selects the combined results of all the specified selectors A
class,.class $(".primary,.secondary") It will select all elements with the class "primary" o
:first-child $("p:first-child") It will select all p elements that are the first child o
:first-of-type $("p:first-of-type") It will select all p elements that are the first p ele
parent
:last-child $("p:last-child") It will select all p elements that are the last child of
:last-of-type $("p:last-of-type") It will select all p elements that are the last p ele
parent
:nth-child(n) $("p:nth-child(2)") This will select all p elements that are the 2nd
parent
:nth-last-child(n) $("p:nth-last-child(2)") This will select all p elements that are the 2nd
parent, counting from the last child
:nth-of-type(n) $("p:nth-of-type(2)") It will select all p elements that are the 2nd p ele
parent
:nth-last-of- $("p:nth-last-of- This will select all p elements that are the 2nd
type(n) type(2)") their parent, counting from the last child
:only-child $("p:only-child") It will select all p elements that are the only child o
:only-of-type $("p:only-of-type") It will select all p elements that are the only child,
their parent
parent > child $("div > p") It will select all p elements that are a direct c
element
parent $("div p") It will select all p elements that are descenda
descendant element
element + next $("div + p") It selects the p element that are next to each div el
element ~ siblings $("div ~ p") It selects all p elements that are siblings of a div el
:eq(index) $("ul li:eq(3)") It will select the fourth element in a list (index start
:gt(no) $("ul li:gt(3)") Select the list elements with an index greater than
:lt(no) $("ul li:lt(3)") Select the list elements with an index less than 3
:not(selector) $("input:not(:empty)") Select all input elements that are not empty
:contains(text) $(":contains('Hello')") Select all elements which contains the text "Hello"
[attribute!=value] $("[href!='default.htm']") It will select all elements with a href attribute value
"default.htm"
[attribute$=value] $("[href$='.jpg']") It will select all elements with a href attribute valu
".jpg"
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:
No Method Description
.
2 clearQueue() It is used to remove all remaining queued functions from the selected eleme
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
7) fadeto() adjusts opacity for the matched element. In other words, it fades in/out
elements.
8) fadetoggle() shows or hides the matched element. In other words, toggles between the
fadeOut() methods.
9) finish() It stops, removes and complete all queued animation for the selected eleme
11) queue() shows or manipulates the queue of methods i.e. to be executed on the selec
14) slidetoggle() shows or hides the matched elements with slide. In other words, it is us
between the slideUp() and slideDown() methods.
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 t
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.
Pause
Unmute
Current Time 2:02
Duration 18:10
Loaded: 16.88%
Â
Fullscreen
1. <!DOCTYPE html>
2. <html>
3. <head>
4. <script src="http://ajax.googleapis.com/ajax/libs/jquery/1.11.2/jquery.min.js"></
script>
5. <script>
6. $(document).ready(function(){
7. $("#hide").click(function(){
8. $("p").hide();
9. });
10. });
11. </script>
12. </head>
13. <body>
14. <p>
15. <b>This is a little poem: </b><br/>
16. Twinkle, twinkle, little star<br/>
17. How I wonder what you are<br/>
18. Up above the world so high<br/>
19. Like a diamond in the sky<br/>
20. Twinkle, twinkle little star<br/>
21. How I wonder what you are
22. </p>
23. <button id="hide">Hide</button>
24. </body>
25. </html>
Test it Now
Output:
Hide
← Prev
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);
speed: It is an optional parameter. It specifies the speed of the delay. Its possible
vales are slow, fast and milliseconds.
1. <!DOCTYPE html>
2. <html>
3. <head>
4. <script src="http://ajax.googleapis.com/ajax/libs/jquery/1.11.2/
jquery.min.js"></script>
5. <script>
6. $(document).ready(function(){
7. $("#hide").click(function(){
8. $("p").hide();
9. });
10. $("#show").click(function(){
11. $("p").show();
12. });
13. });
14. </script>
15. </head>
16. <body>
17. <p>
18. <b>This is a little poem: </b><br/>
19. Twinkle, twinkle, little star<br/>
20. How I wonder what you are<br/>
21. Up above the world so high<br/>
22. Like a diamond in the sky<br/>
23. Twinkle, twinkle little star<br/>
24. How I wonder what you are
25. </p>
26. <button id="hide">Hide</button>
27. <button id="show">Show</button>
28. </body>
29. </html>
Test it Now
Output:
Hide Show
1. $(document).ready(function(){
2. $("#hide").click(function(){
3. $("p").hide(1000);
4. });
5. $("#show").click(function(){
6. $("p").show(1500);
7. });
8. });
Test it Now
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);
speed: It is an optional parameter. It specifies the speed of the delay. Its possible
vales are slow, fast and milliseconds.
1. <!DOCTYPE html>
2. <html>
3. <head>
4. <script src="http://ajax.googleapis.com/ajax/libs/jquery/1.11.2/
jquery.min.js"></script>
5. <script>
6. $(document).ready(function(){
7. $("button").click(function(){
8. $("div.d1").toggle();
9. });
10. });
11. </script>
12. </head>
13. <body>
14. <button>Toggle</button>
15. <div class="d1" style="border:1px solid black;padding:10px;width:250px">
16. <p><b>This is a little poem: </b><br/>
17. Twinkle, twinkle, little star<br/>
18. How I wonder what you are<br/>
19. Up above the world so high<br/>
20. Like a diamond in the sky<br/>
21. Twinkle, twinkle little star<br/>
22. How I wonder what you are</p>
23. </div>
24. </body>
25. </html>
Test it Now
Output:
Toggle
1. $(document).ready(function(){
2. $("button").click(function(){
3. $("div.d1").toggle(1500);
4. });
5. });
Test it Now
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);
speed: It is an optional parameter. It specifies the speed of the delay. Its possible
vales are slow, fast and milliseconds.
1. <!DOCTYPE html>
2. <html>
3. <head>
4. <script src="http://ajax.googleapis.com/ajax/libs/jquery/1.11.2/
jquery.min.js"></script>
5. <script>
6. $(document).ready(function(){
7. $("button").click(function(){
8. $("#div1").fadeIn();
9. $("#div2").fadeIn("slow");
10. $("#div3").fadeIn(3000);
11. });
12. });
13. </script>
14. </head>
15. <body>
16. <p>See the fadeIn() method example with different parameters.</p>
17. <button>Click to fade in boxes</button><br><br>
18. <div id="div1" style="width:80px;height:80px;display:none;background-
color:red;"></div><br>
19. <div id="div2" style="width:80px;height:80px;display:none;background-
color:green;"></div><br>
20. <div id="div3" style="width:80px;height:80px;display:none;background-
color:blue;"></div>
21. </body>
22. </html>
Test it Now
Output:
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);
speed: It is an optional parameter. It specifies the speed of the delay. Its possible
vales are slow, fast and milliseconds.
1. <!DOCTYPE html>
2. <html>
3. <head>
4. <script src="http://ajax.googleapis.com/ajax/libs/jquery/1.11.2/
jquery.min.js"></script>
5. <script>
6. $(document).ready(function(){
7. $("button").click(function(){
8. $("#div1").fadeIn();
9. $("#div2").fadeIn("slow");
10. $("#div3").fadeIn(3000);
11. });
12. });
13. </script>
14. </head>
15. <body>
16. <p>See the fadeIn() method example with different parameters.</p>
17. <button>Click to fade in boxes</button><br><br>
18. <div id="div1" style="width:80px;height:80px;display:none;background-
color:red;"></div><br>
19. <div id="div2" style="width:80px;height:80px;display:none;background-
color:green;"></div><br>
20. <div id="div3" style="width:80px;height:80px;display:none;background-
color:blue;"></div>
21. </body>
22. </html>
Test it Now
Output: