I have a variable with a jQuery collection in it, like this:
$collection = $(".example");
This gives me a bunch of nodes with the class “example”.
Now, I want to manipulate that collection and retain the results, which I thought would work like this:
$(".example", $collection).filter(":even").addClass("even");
Unfortunately, that doesn’t seem to work. Anytime I add an assignment to it, like this:
$collection = $(".example", $collection).filter(":even").addClass("even");
I get an empty collection.
The end result I want is to have all of the .example class even items in the $collection collection to have the class “even”. I don’t want to replace the contents of $collection with only the even .example elements.
Try:
When you defined
$collection = $(".example");what you were doing with$(".example", $collection)was looking for items with the example class inside of items with the example class.Better yet:
… would be even little more direct.
Update:
Doing this won’t overwrite the
$collectionvariable.