Select child P under div tag - Javascript jQuery Method and Property

Javascript examples for jQuery Method and Property:children

Description

Select child P under div tag

Demo Code

ResultView the demo in separate window

<!DOCTYPE html>
<html>
<head>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.2.1/jquery.min.js"></script>
<script>
$(document).ready(function(){
    $("div").children("p").css("background-color", "yellow");
});/*from w  w w .j  a  v  a 2  s. c  o  m*/
</script>
</head>
<body>

<p>This is a p element in body.</p>

<div style="border:1px solid;">
  <p>This is a p element (child of div).</p>
  <h4>This is a h4 element inside the div (child of div).</h4>
  <p class="child">This is a p element with class "child" (child of div).</p>
  <span>This is a span element (child of div).</span>
  <p>This is also a p element (child of div).</p>
</div>

<p>This is also a p element in body.</p>

<div style="border:1px solid;">
  <p>This is a p element (child of div).</p>
  <h4>This is a h4 element inside the div (child of div).</h4>
  <p class="child">This is a p element with class "child" (child of div).</p>
  <span>This is a span element (child of div).</span>
  <p>This is also a p element (child of div).</p>
</div>

</body>
</html>

Related Tutorials