ghaschel
ghaschel

Reputation: 1355

Select everything that have any extra classes different from 2 selectors (jQuery)

I need to select everything that have any extra class than these selectors .not-submenu.separator:not('.first-item'), .first-item.not-submenu.separator how may i apply a wildcard and a ORto this situation?

<li class="first-item not-submenu separator"></li>
<li class="not-submenu separator"></li>
<li class="not-submenu separator randomclass1"></li>//remove everything that have an extra class and still matches the other selectors
<li class="not-submenu separator randomclass2"></li>
<li class="not-submenu separator randomclass3"></li>
(...)
<li class="not-submenu separator randomclassn"></li>

Upvotes: 0

Views: 76

Answers (1)

tandrewnichols
tandrewnichols

Reputation: 3466

You can pass a function to the filter method and return true or false to indicate whether to keep the selected element. Something like this:

$(".not-submenu.separator").filter(function(index) {
    // 'this' refers to the current element
    // 'index' is fairly useless but I wanted to demonstrate the API
    return ! $(this).hasClass('something-else');
});

That should give you a list of element that match your selector that DON'T have the class "something-else." Incidentally, it was hard to figure out what your actual selector was (which elements you were hoping to end up with), so I went with something simple and common to your two examples.

Upvotes: 1

Related Questions