JavaScript – getElementsByTagName

getElementsByTagName is used to get tag information in document.

This is in form of collection and array of tags. We can work with individual tag of HTML in webpage with getElementsByTagName method.

We will show you to how to use this method in webpage

We can get value inside of tag with particular single tag.

document.getElementsByTagName("p").item(0).innerHTML

This will pick first item tag of paragraph with numeric value and get innerHTML of this p tag.

document.getElementsByTagName("p").length

This will return the number of p tags in full document, if you are using 5 paragraph tags inside document. It will return 5.

document.getElementsByTagName("p").namedItem("pID")

This will work with individual element id, and get the property of element. We can get style property, font property

document.getElementsByTagName("p").namedItem("pID").style.background

 

document.getElementsByTagName("p").namedItem("pID").style.font

Full code example

<html>
<head>
<title>getElementsByTagName Javascript</title>
<script>
function getElementFtn()
{
 alert(document.getElementsByTagName("p").item(0).innerHTML)
}
</script>
</head>

<body>
<p>this is first tag</p>

<p>this is second</p>

<p id="pID">this is third tag</p>

<p>this is forth tag</p>

<p>this is fifth tag</p>

<a href="javascript:getElementFtn()">check this element</a>
</body>
</html>
Bookmark  

 

Leave a Reply

Security Code:

 

  Random Post