Skip to content

Why this is not working in :not() in jquery

I’m currently facing this problem where :not(this) is not working but is working when I specify the exact element selector. Please see my code sample below.

$(document).ready(function(){
  $('p').on('click', function(){
  
    $('p:not(this, .intro)').css('background', 'yellow');
    
  });
});
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/2.1.4/jquery.min.js"></script>


<p class="intro">My name is Donald.</p>
<p>I live in Duckburg.</p>
<p>My best friend is Mickey.</p>

Answer

$(document).ready(function(){
  $('p').on('click', function(){
    $('p:not(.intro)').not(this).css('background', 'yellow');    
  });
});
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/2.1.4/jquery.min.js"></script>


<p class="intro">My name is Donald.</p>
<p>I live in Duckburg.</p>
<p>My best friend is Mickey.</p>