jQuery Selector How to - Select all children of the element with id "main"








Question

We would like to know how to select all children of the element with id "main".

Answer


<!--from   ww w.  j a  va2s .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(){
           $("#main > *").css("border", "3px solid red");
        });
    </script>
  </head>
  <body>
      <span id="main">
        <div>asdf</div>
        <button>Child</button>
        <span>A Span</span>
      </span>
    </body>
</html>

The code above is rendered as follows: