tag names of body element's children in JavaScript?


Javascript has provided document.body.children to get the tag names of the children in the body. This method gives out tag names such as p, script, div, etc.

syntax

var t = document.body.children;

Example

In the following example, in the body, there are tags such as 2 paragraph tags and 1 script tag. When the method document.body.children is applied the names of the tags will be displayed as shown in the output.  

Live Demo

<html>
<body>
<p>Gtting the tag names of the body element's children.</p>
<p id = "body"></p>
<script>
   var c = document.body.children;
   var txt = "";
   for (var i = 0; i < c.length; i++) {
      txt = txt + c[i].tagName + "<br>";
      document.getElementById("body").innerHTML = txt;
   }
</script>
</body>
</html>

Output

Gtting the tag names of the body element's children.
P
P
SCRIPT
raja
Published on 08-Aug-2019 15:02:50
Advertisements