views:

61

answers:

1
var div = $(this), ul = $("ul", div), li = $("li", ul);

Please explain, what does this code do?

By steps.

Thanks.

+10  A: 

It ends up with these equivalents:

var div = $(this);
var ul = $(this).find("ul");
var li = $(this).find("ul").find("li");

So it's getting the current <div>, any <ul> elements inside it, and any <li> elements inside those, and placing each collection in its own variable.

When you do $(selector, content) you're actually doing $(context).find(selector) under the covers, so the code in your question is just chaining one call to the next, effectively doing a .find() inside each time.

Nick Craver
+1 for a fun-to-read and accurate explanation. Is it just me, or is the stupid `$(selector, content)` way of selecting elements a pain in the ass to read? What is wrong with `find`?
karim79
@karim79 - nope not just you, it seems to be counter to the whole fluent/chain style of the library, totally backwards imo...not to mention less efficient.
Nick Craver