jQuery Selector How to - Select div which has a paragraph inside of it








Question

We would like to know how to select div which has a paragraph inside of it.

Answer


<!--   w  ww  . j  ava2  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:has(p)").css("color","red");
        });
    </script>
  </head>
  <body>
      <div><p>Hello in a paragraph</p></div>
  </body>
</html>

The code above is rendered as follows: