jquery traverse all descendants; In this tutorial, you will learn how to do get or find descendant elements using jquery method traversing methods.
jQuery Traversing Descendant
The jQuery Traversing Descendants means a child, grandchild, great-grandchild, and so on.
jQuery offers useful ways to use children () and find (), which you can use to easily find in dom trees at single or multiple levels or to obtain children or other ancestry of an element in the hierarchy.
jQuery children() Method
The jQuery children() method is used to get or find the direct children of the selected element.
Syntax children() Method
$("selector").children()
jQuery children() method By Example
This example will demostrate you how use children() method to selected html elements.
<!DOCTYPE html> <html lang="en"> <head> <meta charset="utf-8"> <title>jQuery children() Method Example</title> <script src="https://code.jquery.com/jquery-3.3.1.min.js"></script> <style type="text/css"> .g_cls{ background: green; } </style> <script type="text/javascript"> $(document).ready(function(){ $(".childM").children().addClass("g_cls"); }); </script> </head> <body> <div class="container"> <h3>Hello World</h3> <p>This is paragraph</p> <p>This is another paragraph.</p> <ul class="childM"> <li>First</li> <li>Second</li> <li>Third</li> </ul> </div> </body> </html>
Output
Hello World
This is paragraph
This is another paragraph.
- First
- Second
- Third
jQuery find() Method
Using the jQuery find() method to obtain descendant elements of the selected element.
Syntax find() Method
$("selector").find()
jQuery find() method By Example
This example will demostrate you how use find method to selected html elements.
<!DOCTYPE html> <html lang="en"> <head> <meta charset="utf-8"> <title>jQuery find() Method Example</title> <script src="https://code.jquery.com/jquery-3.3.1.min.js"></script> <style type="text/css"> .g_cls{ background: #28BAA2; } </style> <script type="text/javascript"> $(document).ready(function(){ $(".container-find").find("li").addClass("g_cls"); }); </script> </head> <body> <div class="container-find"> <h3>Hello World</h3> <p>This is paragraph</p> <p>This is another paragraph.</p> <ul class="childM"> <li>First</li> <li>Second</li> <li>Third</li> </ul> </div> </body> </html>
Output
Hello World
This is paragraph
This is another paragraph.
- First
- Second
- Third
Recommended jQuery Tutorials
- jquery keyup event example
- Jquery Click() Event Method with E.g.
- Event jQuery. Blur By Example
- jQuery form submit event with example
- jQuery Keypress() Event Detect ENTER key pressed
- keydown function jQuery
- List of jQuery Events Handling Methods with examples
- Jquery Selector by .class | name | #id | Elements
- How to Get the Current Page URL in jQuery
- jQuery Ajax Get() Method Example
- get radio button checked value jquery by id, name, class
- jQuery Set & Get innerWidth & innerHeight Of Html Elements
- jQuery Get Data Text, Id, Attribute Value By Example
- Set data attribute value jquery
- select multiple class in jquery
- How to Remove Attribute Of Html Elements In jQuery
- How to Checked Unchecked Checkbox Using jQuery
- jQuery removeClass & addClass On Button Click By E.g.
Thanks for sharing useful information.