Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Using an array with jQuery's .not()

Tags:

arrays

jquery

What I'm trying to learn is how to use an array with .not() in jQuery. My code is below. Do you know why .item-ii is not being excluded from the selection? Thanks in advance!

<ul class="level-1">
  <li class="item-i">I</li>
  <li class="item-ii">II
    <ul class="level-2">
      <li class="item-a">A</li>
      <li class="item-b">B
        <ul class="level-3">
          <li class="item-1">1</li>
          <li class="item-2">2</li>
          <li class="item-3">3</li>
        </ul>
      </li>
      <li class="item-c">C</li>
    </ul>
  </li>
  <li class="item-iii">III</li>
</ul>
<script>
    var temp = new Array();
    temp[0] = '.item-ii';
    console.log(temp);
    $('li').not(temp).css('background', 'red');
</script>
like image 919
Tom Avatar asked Jul 29 '26 22:07

Tom


1 Answers

If you need to exclude all the selectors in the temp array, you can do:

$('li').not(temp.join(',')).css('background', 'red');

For example if temp = ['.item-ii', '.item-i'], the above will be equivalent to:

$('li').not('.item-ii,.item-i').css('background', 'red');
like image 126
techfoobar Avatar answered Aug 01 '26 11:08

techfoobar



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!