Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

jQuery chaining: Can everything be chained? When can we not chain?

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.

like image 490
Pinkie Avatar asked Mar 31 '11 20:03

Pinkie


People also ask

Can jQuery be chained?

With jQuery, you can chain together actions/methods. Chaining allows us to run multiple jQuery methods (on the same element) within a single statement.

Which function is used to chain animations in jQuery?

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.

What is chaining in JavaScript?

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.

What is this keyword in jQuery?

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.


1 Answers

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: enter image description here

while .height() cannot, because it returns an integer.

enter image description here

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.)

like image 167
kennytm Avatar answered Sep 18 '22 14:09

kennytm