Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

jQuery: Check if jQuery object contains (includes) exact DOM element

I have a reference to a DOM element, and a jQuery object which is the result of a selector, and I want to check if that specific DOM element is in that jQuery object. Short of looping through the whole jQuery object and checking for equality, is there a straightforward way in jQuery to do this?

I have tried .contains, :contains, .has and :has, and none of them seem to do the job. Also, I should mention that all the elements I'm working with are on the same DOM tree level, so there is no need to worry about parents/children.

like image 234
Suan Avatar asked Feb 07 '10 20:02

Suan


2 Answers

$yourJqueryObject.is(yourDomElement)

See .is() added in 1.6.

like image 99
thorn0 Avatar answered Sep 28 '22 10:09

thorn0


similar to Gumbos answer, but slimmer:

if ( obj.filter(function() { return this == el; }).length ) {
    // obj contains el
}
like image 42
David Hellsing Avatar answered Sep 28 '22 10:09

David Hellsing