jQuery | chaining()

jQuery is a very powerful framework of JavaScript. With jQuery, we can use do chaining which means to chain together multiple methods in a single statement on a single element.
We have been using single statement at once, but now using chaining method we can bind multiple methods to make the code short. This way, browsers do not have to find the same element(s) more than once.

Advantage:
While using method chaining in jQuery, it ensures that there is no need to use the same selector more than once. Over-using a selector can seriously slow down your code, as every time you call on a selector you are forcing the browser to go looking for it. By combining or “chaining” multiple methods, you can seriously cut down on the number of times you make your browser look for the same elements without having to set any variables.

Implementation of chaining() using JavaScript and jQuery for better understanding:

Code #1: Using Slide up and slide down method functions for chaining-



filter_none

edit
close

play_arrow

link
brightness_4
code

<!DOCTYPE html>
<html>
  
<head>
                 jquery/3.3.1/jquery.min.js"></script>
  
</head>
  
<body>
  
    <p id="para">Chaining method in jQuery</p>
  
    <button>Click me</button>
  
    <script type="text/javascript">
        $(document).ready(function() {
            $("button").click(function() {
                  
                 //assigning the color blue
                $("#para").css("color", "blue")
                //using slide up method
                    .slideUp(2000)  
                 //using slide down method
                    .slideDown(2000).
                slideUp(2000).
                slideDown(4000);
            });
        });
    </script>
</body>
  
</html>

chevron_right


Output:
Before clicking on “Click me” button-

After clicking on “Click me” button-
First CSS property will be applied and the text colour will be changed.

After slideUp function, the text will be hidden by the button.

After slideDown function, the text will again appear slowly till 4000 milliseconds.

Code #2: Using animate function in jQuery for chaining effect-

filter_none

edit
close

play_arrow

link
brightness_4
code

<!DOCTYPE html>
<html>
  
<head>
                 jquery/3.3.1/jquery.min.js"></script>
  
</head>
  
<body>
  
    <p id="para">Chaining method in jQuery</p>
  
    <button>Click me</button>
  
    <script type="text/javascript">
       $(document).ready(function() {
        $("button").click(function() { 
         $("#para").css("color", "blue").animate({
               width: "100%"
            })
            //it adds animation to the program by styling it
            .animate({
               fontSize: "46px"
             })
            .animate({
                borderWidth: 30
             });
            });
        });
    </script>
</body>
  
</html>

chevron_right


Output:
Before clicking on “Click me” button-

After clicking on “Click me” button-
The size gets bigger due to animate property.



My Personal Notes arrow_drop_up

Check out this Author's contributed articles.

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.




Article Tags :

Be the First to upvote.


Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.