Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I target elements whose classes don't contain the ID of this element?

Tags:

jquery

Specifically, I want to say: for elements whose class value doesn't contain this element's ID value, execute this function.

For example, clicking #foo will execute fadeOut() on the list items whose classes don't contain .foo in the following HTML:

<button id="foo">foo</button>
<button id="bar">bar</button>
<button id="baz">baz</button>
<ul>
    <li class="foo">Lorem</li>
    <li class="foo bar">Ipsum</li>
    <li class="baz">Dolor</li>
</ul>

So upon clicking #foo, the last list item should disappear, since the first two both contain the .foo class.

like image 311
Chris Avatar asked Dec 24 '12 01:12

Chris


1 Answers

Try

$('button').on('click', function(){
    $('li:not(.'+this.id+')').fadeOut();
});

Demo

like image 189
Musa Avatar answered Nov 09 '22 10:11

Musa