John the Painter
John the Painter

Reputation: 2615

jQuery .filter() elements that do not have variable

How can I turn this:

activeSelector = '.active,.selected,.current';

$menuChildren.filter(activeSelector).find('.each-project-content-container').remove();

into a filter that is not activeSelector?

Some ideas that didn't work so you get the idea:

$menuChildren.filter(!activeSelector).find('.each-project-content-container').remove();
$menuChildren.filter(!=activeSelector).find('.each-project-content-container').remove();
$menuChildren.not(filter(activeSelector)).find('.each-project-content-container').remove();

Upvotes: 0

Views: 67

Answers (2)

mgerton
mgerton

Reputation: 31

Try this:

$menuChildren.filter(function(index) {
    return $(this).not(activeSelector).find('.each-project-content-container').remove();
});

This will do a check on each element that gets passed through the filter and look for an element that doesn't have the activeSelector string. If it finds a matching element, then it'll remove the .each-project-content-container child elements.

EDIT:

Actually, you don't even need the filter() call here. You can simply select all of the non-active elements, find their children of the specified class, and remove:

$menuChildren.not(activeSelector).find('.each-project-content-container').remove();

That functions the same as above, and is still a one-liner.

Upvotes: 0

Jason P
Jason P

Reputation: 27012

You could use not():

$menuChildren.not(activeSelector)

Upvotes: 1

Related Questions