[jQuery] Tip: jQuery chains
jQuery chains are one of the best features of jQuery, but did you know
that the code doesn't have to be on the same line? For example take
this chain (totally fictional, but I think it demonstrates the point):
$("table").each(addToList).addClass("zebratable").find("tr").mouseover(highlight).mouseout(unhighlight).find("td:even").addClass("even").end().find("td
a").click(showpopup).end().end().fadeIn();
Can be quite hard to decipher code like that. Thankfully there is a better way:
$("table")
// add to list of tables
.each(addToList)
// style it like a zebra table
.addClass("zebratable")
// get rows
.find("tr")
// apply row highlight function
.mouseover(highlight)
// apply row unhighlight
.mouseout(unhighlight)
// get even cells
.find("td:even")
// apply even class
.addClass("even")
// finished with even cells
.end()
// get all links within table cells
.find("td a")
// make them show a popup rather than going to the page
.click(showpopup)
// finished with links
.end()
// finished with table rows
.end()
// fade in the table
.fadeIn();
I have used this method before (found it out by accident) and it
allows you to use a chain while still allowing comments. By biggest
chain has been 6 so far, but I could do longer ones in the future.
_______________________________________________
jQuery mailing list
discuss@jquery.com
http://jquery.com/discuss/