Welcome to the Treehouse Community

The Treehouse Community is a meeting place for developers, designers, and programmers of all backgrounds and skill levels to get support. Collaborate here on code errors or bugs that you need feedback on, or asking for an extra set of eyes on your latest project. Join thousands of Treehouse students and alumni in the community today. (Note: Only Treehouse students can comment or ask questions, but non-students are welcome to browse our conversations.)

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and a supportive community. Start your free trial today.

JavaScript

Jonathan Grieve
MOD
Jonathan Grieve
Treehouse Moderator 90,705 Points

Question about jQuery chaining.

I've another jquery related questoon for you all.

I've got 2 examples here of method chaining in jQuery which I understand to be 2 or more methods bound to an element.

This one from the lightbox example.

var captionText = $(this).children("img").attr("alt");

And this example I found from the documentation.

$("#person").slideDown('slow')
   .addClass('grouped')
   .css('margin-left', '11px');

My question is this, is there a limit to how many methods you can put on a jQuery object? Do we have to worry about putting these methods in a particular order for them to work?

3 Answers

Nicholas Olsen
seal-mask
.a{fill-rule:evenodd;}techdegree
Nicholas Olsen
Front End Web Development Techdegree Student 19,342 Points

Technically there isn't a limit to how many methods you can chain to a jQuery object.

Your second question is more difficult to answer. Most of the time I don't think you'll need to worry what order you call each method in but I also suspect there will be a few exceptions. For example, jQuery has a "clearQueue" method that may have to be called at just the right times. Its hard to generalize, but I think the answer is "No, most of the time."

Robert Bojor
PLUS
Robert Bojor
Courses Plus Student 29,439 Points

Hi Jonathan,

As far as I worked with jQuery and Javascript lately I never needed more than 4 or maximum 5 in a row - I don't know of a limit that applies but, technically, there shouldn't be a limit.

As far as the order of the chain goes, you have to put them in the order you need/desire, they will be executed in that order.

Jonathan Grieve
MOD
Jonathan Grieve
Treehouse Moderator 90,705 Points

So the answer to take from this is "No" but when it is, learn from that experience. I consider my question answered, thanks. :-)