.getElementById();

To access a unique element theĀ .getElementById(); method should be used.

 

var myElement = document.getElementById("myId"); //assigns a handle to the node containing id="myId"

 

 

This example assigns the element with an id of myHeading to a variable (handle) and then displays the text (using innerHTML) within that element:

<html>
    <head>
        <title>JavaScript</title>
    </head>
    <body>
		<h1 id="myId">An h1 tag element with an id of myHeading</h1>
		<script>
			var myHead = document.getElementById("myId");

			document.write(myHead.innerHTML);
		</script>
    </body>
</html>

Save & refresh browser:

An h1 tag element with an id of myHeading

An h1 tag element with an id of myHeading

Leave a Reply