jQuery Selector parent descendant

Introduction

The ("parent descendant") selector selects elements which are descendants of a specified element.

A descendant of an element could be a child, grandchild, great-grandchild, etc, of that element.

("parent descendant")
Parameter OptionalDescription
parent Required. parent element to be selected
descendant Required. descendant element to be selected

Select all <span> elements that are descendants of a <div> element:

View in separate window

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

<div style="border:1px solid black;padding:10px;">
  <p>This is a paragraph.</p>
  <span>This is a text inside a span.</span>
</div>

<div style="border:1px solid black;padding:10px;">
  <p>This is a paragraph. <span>This is a span 
     element, inside the paragraph.</span></p>
</div>

</body>
</html>



PreviousNext

Related