Hello, how does the getElementbyTagname and the length of that works ?
Suppose my codes is :

<!DOCTYPE html>
<html>
<body>

<p id="demo">This is demo</p>
<div id="original">This is original</div>

<button onclick="myFunction()">Try it</button>

<script>

function myFunction()
{
var elements = document.getElementsByTagName("*");
alert(elements.length);
};

</script>

</body>
</html>

Output is 7. Why ?

Besides that, sometimes getElementByTagname("P")[0] end with the array, izit determine the position of the tagName ?

Recommended Answers

document.getElementsByTagName("*") returns all the tags. Following is the list which it returns

  • html
  • head
  • body
  • p#demo
  • div#original
  • button
  • script
Jump to Post

All 2 Replies

document.getElementsByTagName("*") returns all the tags. Following is the list which it returns

  • html
  • head
  • body
  • p#demo
  • div#original
  • button
  • script

it gives number of tags present in your file except the tag in witch you declare function

Be a part of the DaniWeb community

We're a friendly, industry-focused community of 1.19 million developers, IT pros, digital marketers, and technology enthusiasts learning and sharing knowledge.