Nick
Nick

Reputation: 6025

jQuery: $('#id').find('> a')... finds what, exactly?

I'm sure someone will change my title, but I liked it while it lasted.

Anyway, what the Hector does this do (as my seven-year-old would say)?

$('#id').find('> a')...

BTW, I get that $('#parent > .child') is the same as $('#parent').children('.child') (as explained here), not that I ever use it.

Upvotes: 1

Views: 272

Answers (3)

Alnitak
Alnitak

Reputation: 339917

Using > a without a specified parent is actually deprecated syntax, since as you've discovered it has ambiguous semantics. .find('> a') is actually equivalent to .children('a').

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.

[ Remembering that $(sel, context) is equivalent to $(context).find(sel) ]

Upvotes: 3

Tarun Dudhatra
Tarun Dudhatra

Reputation: 76

While you are trying to find an element using below query

$("#master").find('> a') 

then it will find all the first level anchor element inside the element which has attribute id = 'master'

And while you are trying to find an element using below query

$("#master").find('a')

then it will find all the anchor element inside the element which has attribute id = 'master' no matter it is at what level.

Upvotes: 1

Derek Hunziker
Derek Hunziker

Reputation: 13141

.find() is usually used to select descendants many levels down, however, in your case, adding the > before the a limits the selection to children only (or first level only if your using CSS class in place of #id).

It is essentially the same as $('#id > a') or $('#id').children('a') but with an extra function call.

Your title is perfect, BTW :)

Upvotes: 1

Related Questions