children() Method - Javascript jQuery Method and Property

Javascript examples for jQuery Method and Property:children

Description

The children() method returns all direct children of the selected element.

Syntax

Parameter Require Description
filterOptional. selector expression to narrow down the search for children

The following code shows how to Return elements that are direct children of <ul>:

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(){
    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>

Related Tutorials