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"...
views:
30answers:
3
+1
A:
There is closest. The only thing with this is that it propagates up the DOM.
Dustin Laine
2010-08-13 18:34:52
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
2010-08-13 18:44:51
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
2010-08-13 20:55:22
I guess there is no such function and I'll need to implement it myself using children() recursively. TNX.
Konrad Eisele
2010-08-13 21:07:56
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
2010-08-13 18:45:56
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
2010-08-14 17:10:42
A:
something like this might do
$jqObj.find( '.dummy:eq(0)' )
This would return the first matched element.
ovais.tariq
2010-08-13 19:07:08
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
2010-08-13 19:49:44