Banchan Kiro Banchan Kiro - 1 year ago 97
jQuery Question

jQuery (advanced?) function

I have found many example jQuery code where they use multiple functions like


Can someone please explain to me how this actually works and how to properly use it?

Answer Source

Assuming jQuery, $(selector) returns a jQuery collection object. jQuery defines many methods on jQuery collection object, such as attr, find, css or slideUp, which will commonly return a jQuery collection object (same or different). This is commonly called "method chaining". For example,

$('div').css('background-color', 'lime').after('<div>More</div>');
<script src=""></script>

the first part, $('div'), returns a jQuery collection containing our single <div>; .css(...) will set its background and return the same collection; .after(...) will insert the new <div> after the first one and again return the same collection.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download