Javascript examples for jQuery Method and Property:children
The children() method returns all direct children of the selected element.
Parameter | Require | Description |
---|---|---|
filter | Optional. | selector expression to narrow down the search for children |
The following code shows how to Return elements that are direct children of <ul>:
<!DOCTYPE html> <html> <head> <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.2.1/jquery.min.js"></script> <script> $(document).ready(function(){ var treeTraversal = $("div").children().map(function(){ return this.tagName;}).get().join(", "); $("div").after("<h2>" + treeTraversal + "<h2>"); });//from w w w . j a v a2s . co m </script> </head> <body> <div> This is a div element. My child is: <p> <span></span> </p> </div> </body> </html>