jQuery Selector How to - Select all p elements that are children of a div element








Question

We would like to know how to select all p elements that are children of a div element.

Answer


<!--from   w ww .  ja  v  a2  s .  c  om-->
<html>
  <head>
    <script type="text/javascript" src='http://code.jquery.com/jquery-1.5.2.js'></script>
    <script type="text/javascript">
        $(document).ready(function(){
            $("div > p").css("border", "1px solid gray");   
        });
    </script>
  </head>
  <body>
        <div><p>asdf</p></div>
  </body>
</html>

The code above is rendered as follows: