Reputation: 2042
I initialize a jQuery collection like so
$collection = $([])
later on some things happend and I do a lot of
$collection = $collection.add($element)
I do this so that I can call functions like $collection.hide()
on all of the elements at once.
The problem is that later I want to do something like _.contains($collection, $element)
but this doesn't work.
For example:
var $b = $("#content")
var $c = $([]).add(b)
console.log( _.contains($c, $b) )
console.log( _.contains($c.toArray(), $b)
Both evaluate to false.
How can I accomplish my goal with jQuery?
Upvotes: 2
Views: 648
Reputation: 437386
With .is
:
if ($c.is($b)) // $c contains $b
Note that .is
goes out of its way to make life easy: you can pass a selector (e.g. "#content"
), or an element (e.g. document.getElementById("content")
), or a jQuery object (e.g. $("#content")
) and it will work as expected in each case.
Upvotes: 6