Learn Chaining in jQuery


In the previous lesson, readers read along with Taimienphi.vn to learn about the stop () method in jQuery. In the next lesson, Taimienphi.vn will introduce you to Chaining in jQuery.


jQuery includes a powerful feature called Chaining, which allows users to run multiple jQuery methods in a single command line. To learn more about Chaining in jQuery, read along with the jQuery lesson below of Taimienphi.vn.

Learn Chaining in jQuery

The Chaining method in jQuery

As mentioned above, jQuery includes a powerful feature called Chaining, which allows us to perform various actions on the same set of elements, in other words, to run different methods in a single command line.

This is because most jQuery methods return an object that can be used to call other methods.

For example: The following example illustrates how to use the Chaining method in jQuery:

chaining in jquery

The output will look like the following:

chaining in jquery 2

The example above illustrates the sequence of 3 methods animate (). When the user clicks the trigger button, it expands the width of the element p is 100%. When the value of width change, font-size will start to create animation, after it’s done border will start creating animations.

Tips: In addition to making it easier for users to read and write code, the chaining method also improves script performance because the browser does not have to search the same elements multiple times.

In addition, if you want, we can split a single line of code into multiple lines to make it easier to read the code.

For example: The method strings in the example above can be written as follows:

chaining in jquery 3

The output will look like the following:

chaining in jquery 4

Some jQuery methods do not return jQuery objects. Basically, setter methods or methods that assign some values ​​to the selected element return the jQuery object, allowing us to call jQuery methods.

In contrast, the getter methods that return the requested value do not allow calling jQuery methods on the return value of the getter.

Typically as a method html (). If no parameters are assigned to the method, the HTML content of the selected element will be returned instead of the jQuery object, such as in the example below:

chaining in jquery 5

The output will look like the following:

chaining in jquery 6

The above lesson Taimienphi.vn has just introduced you to Chaining in jQuery. In the next lesson, Taimienphi.vn will introduce you further jQuery callback function.

https://thuthuat.taimienphi.vn/chaining-rong-jquery-51488n.aspx
Also readers can refer to some other lessons already on Taimienphi.vn to learn more about Slide effects in jQuery.

.

Add a Comment

Your email address will not be published. Required fields are marked *