jQuery Method How to - Use andSelf to add current node








Question

We would like to know how to use andSelf to add current node.

Answer


<html>
  <head>
    <script type="text/javascript" src='http://code.jquery.com/jquery-1.5.2.js'></script>
    <script type="text/javascript">
        $(document).ready(function(){<!--from  w ww.j a v  a2 s .c  om-->
             $("div").andSelf().css("background", "yellow");
        });
    </script>
  </head>
  <body>
          <div>asdf</div>
          <div>asdf</div>
          <div>asdf</div>
          <div class="selected">asdf</div>
          <div>asdf</div>
  </body>
</html>

The code above is rendered as follows: