Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

what does this mean: "jQuery('> li', this)"

I'm trying to figure out how this jQuery plugin works: http://codeasily.com/jquery/multi-column-list-with-jquery

In the plugin there is this line at the beginning:

 if(jQuery('> li', this)) {

I know what

ul > li

means: it means select all li whose direct parent is a ul. But what does '> li' mean? I ran:

$('> li')

but it returns

[]

even though I have plenty of nested unordered list HTML on the page.

like image 837
Homan Avatar asked Jan 17 '12 18:01

Homan


4 Answers

Don't use it. The docs advise that you shouldn't use at as it will be soon deprecated.

From http://api.jquery.com/child-selector/

Note: The $("> elem", context) selector will be deprecated in a future release. Its usage is thus discouraged in lieu of using alternative selectors.


But to answer, it effectively uses the element(s) from which the selector is called as the left hand operand of the child-selector.

like image 95
user1106925 Avatar answered Nov 17 '22 14:11

user1106925


The 2nd parameter to the jQuery function is the context.

jQuery('> li', this)

Is the same as:

jQuery(this).find('> li')
like image 38
Rocket Hazmat Avatar answered Nov 17 '22 13:11

Rocket Hazmat


It's the same as $(this).children('li'). It's basically saying "use this as the context for the selector (> li)".

like image 3
maxedison Avatar answered Nov 17 '22 14:11

maxedison


its jsut like ul > li except that in this case you are replacing the ul part with the current context which is this. So whatever this is in the scope of that call that is the element you are resolving the > li to.

So for example:

var ele = $('ul#someId');
var list = $('> li', ele);


var list2 = $('ul#someId > li');

// list is the same as list2
like image 1
prodigitalson Avatar answered Nov 17 '22 14:11

prodigitalson