JQuery "find" Method Alternative
Solution 1:
jQuery find
gets the descendants of each element in the current set of matched elements, filtered by a selector, jQuery object, or element.
children
gets the children of each element in the set of matched elements, optionally filtered by a selector.
I think you are trying to find the elements at the same level then you should use children
. Alternatively you can also use filter
to filter the matched results based on selector.
filter
reduces the set of matched elements to those that match the selector or pass the function's test.
Try this
var div = $('.wrapper div').filter('.parent');
Solution 2:
Solution 3:
the alternatives of .find()
function which are as below:
- Child Selector (“parent > child”) it selects only first-level descendants or direct child elements. for example
$('#parent_id > #child_id')
or$(".parent > .first-level-child")
- Descendant Selector (“ancestor descendant”) it selects a child, grandchild, great-grandchild, and so on, of that element. in it you can use
$('#parent_id #child_id')
or$('#parent_id #grandchild_id')
or$(".parent .great-grand-child")
or$( "form input" )
- .filter() only search in those elements that match the precondition.
- .parent() get the parent of each element in the current set of matched elements, optionally filtered by a selector.
- .children() it works exactly the same way as find, but it will only find first-level-children, not more distant descendants.
- .closest() get the closest (first) element that matches the selector, starting at the current element.
for detailed info about jquery selectors check JQuery Selectors
Solution 4:
$('.wrapper a').find('a');
find links inside links that are descendants of .wapprer.
I think you might have meant $('.wrapper').find('a');
. In your fiddle that would be
$('.wrapper').find('.parent');`
insetead of:
$('.wrapper div').find('.parent');
Post a Comment for "JQuery "find" Method Alternative"