jQuery Selector How to - Filter out all elements matching the given selector using not operator








Question

We would like to know how to filter out all elements matching the given selector using not operator.

Answer


<!--from  w  ww. j  ava  2s  .c o  m-->
<html>
  <head>
    <script type="text/javascript" src='http://code.jquery.com/jquery-1.5.2.js'></script>
    <script type="text/javascript">
        $(document).ready(function(){
               $("input:not(:checked) + span").css("background-color", "yellow");
        });
    </script>
  </head>
  <body>
      <div>
        <input type="checkbox" name="a" />
        <span>A</span>
      </div>
      <div>
        <input type="checkbox" name="b" />
        <span>B</span>
      </div>
      <div>
        <input type="checkbox" name="c" checked="checked" />
        <span>C</span>
    </div>
    </body>
</html>

The code above is rendered as follows: