I know that not all jQuery functions can be chained together. Is there a rule of thumb on this. When can we not chain 2 functions together.
With jQuery, you can chain together actions/methods. Chaining allows us to run multiple jQuery methods (on the same element) within a single statement.
jQuery | chaining() 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 a single statement at once, but now using the chaining method we can bind multiple methods to make the code short.
Method chaining is an interesting feature in JavaScript, which helps to reduce the size of code and increase the readability of our code. In method chaining, we call separate methods of a single object like a chain without assigning the same object multiple times by assignment operators.
In this article, we will learn the difference between this and $(this) in jQuery. this keyword: In JavaScript, this keyword is used to refer to the object it belongs to. The value that this stores is the current execution context of the JavaScript program.
You can chain when the function returns a "jQuery object".
For example, .css(property, value)
can be chained, as the doc says it Returns jQuery:
while .height()
cannot, because it returns an integer.
Typically, the functions that returns "jQuery objects" are those which typically would not "return a value", e.g. setter methods (.css(prop, val)
, .addClass()
), event binders (.click(handler)
), etc.
(Of course traverse methods (.parent()
, .find()
, etc.) can also be chained but the returned object will be different from the input.)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With