jQuery Selector How to - Select by ID selector only








Question

We would like to know how to select by ID selector only.

Answer


<!--   ww  w.  j  a v a  2 s .c  om-->
<html>
  <head>
    <script type="text/javascript" src='http://code.jquery.com/jquery-1.5.2.js'></script>
    <style type="text/css">
        .changeP { font-weight: bold; color:red;}
    </style>
    <script type="text/javascript">
      $(document).ready(function(){
         $("#myID").css("border","3px solid red");
      });
    </script>
  </head>
  <body>
      <div id="myID">0</div>
      <div id="myID1">1</div>
      <div id="myID2">2</div>
  </body>
</html>

The code above is rendered as follows: