Updated on Kisan Patel
To find out the html elements by their tag names, we need to use JavaScript getElementsByTagName
method.
<script type="text/javascript" language="javascript"> function TestFunction() { alert(document.getElementsByTagName("label")[0].innerHTML); } </script> <label>Hello World!</label> <label>Hello India!</label> <label>Hello USA!</label> <input type="button" id="btnTest" onclick="TestFunction()" value="Click me" />
Javascript getElementsByTagName
method can be used to find all the elements of the page using the tag name. It returns all the elements in the form of array.
In the above function, getElementsByTagName
finds all html elements by tag name. “length” property can be used to know the number of elements found on the page and indexer can be used to access a particular element.
In the above code snippet, we are retrieving the first label of the page and display content inside this label in alert.
See the Pen rVRqQW by Kisan (@pka246) on CodePen.