Homan
Homan

Reputation: 26748

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.

Upvotes: 5

Views: 8597

Answers (6)

user1106925
user1106925

Reputation:

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.

Upvotes: 6

JaredPar
JaredPar

Reputation: 755141

The selector is looking for any immediate <li> children in the context of the this DOM element

Upvotes: 1

gen_Eric
gen_Eric

Reputation: 227280

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

jQuery('> li', this)

Is the same as:

jQuery(this).find('> li')

Upvotes: 5

ShankarSangoli
ShankarSangoli

Reputation: 69915

It will look for li element in the immediate children within this element where this can be a jQuery object or DOM element.

Upvotes: 1

maxedison
maxedison

Reputation: 17573

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

Upvotes: 3

prodigitalson
prodigitalson

Reputation: 60403

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

Upvotes: 1

Related Questions