Definition and usage
The getElementById () method returns a reference to the first object that owns the specified ID.
Grammar
document.getElementById (ID)
Description
The HTML DOM defines a variety of methods for finding elements, with the exception of getElementById () and Getelementsbyname () and getElementsByTagName ().
However, if you need to find a particular element in the document, the most efficient way is getElementById ().
When manipulating a particular element of a document, it is best to give the element an id attribute, assign it a unique name (in the document), and then use that ID to find the element you want.
Example 1
document.getElementById("myHeader") Alert ( x.innerhtml) }</script>
Example 2getElementById () is an important method that is used very often in DOM programming. We have defined a tool function for you so that you can use the getElementById () method with a shorter name:
function ID (x) { document.getElementById(x)
; return x; }
The above function accepts the element ID as their argument. For each of these parameters, you can just write X = ID (x) before using it.
HTML DOM getElementById () method