Tutorialspoint.dev

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-

<!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>

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-

<!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>

Output:
Before clicking on “Click me” button-

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



This article is attributed to GeeksforGeeks.org

leave a comment

code

0 Comments

load comments

Subscribe to Our Newsletter