views:

30

answers:

3

Is it possible to force jquery find() to only return the first matched element and not the childs of that element. Something that would be called "nearest"...

+1  A: 

There is closest. The only thing with this is that it propagates up the DOM.

Dustin Laine
I would want to search for the childs not parents. Same as closest() but the other way, down, the tree. find() doesnt break when finding an child element, but continues deeper. I'd need something of a "break" option to find() to prohibit going deeper.
Konrad Eisele
I think you have some acceptable answers for this above. If you know that it is only one level deep you can use `children` as it is not recursive.
Dustin Laine
I guess there is no such function and I'll need to implement it myself using children() recursively. TNX.
Konrad Eisele
+1  A: 
Thomas Clayson
Yes, something like find(), however if the <li> s are nested it should stop with the level it first finds an entry and not traverse deeper...
Konrad Eisele
No... find() will find all li's contained in the selector... it won't care how deep it will be. To get only DIRECT CHILDREN you need to use the selector parameter ">" (eg: $('ul > li') or $('div#container > div')) and then to find the FIRST DIRECT CHILD you need to use :first (eg: $('ul > li:first') or $('div#container > div:first')). Hope this helps.
Thomas Clayson
A: 

something like this might do

$jqObj.find( '.dummy:eq(0)' )

This would return the first matched element.

ovais.tariq
Not quite. Still the function I search should return a set. Kind of an umbrella of all the first found element in every sub-branch. I'd say that would be the the equivalent to closest() but downward.
Konrad Eisele