The getElementById () method of the html DOM document Object, domgetelementbyid
The getElementById () method returns a reference to the first object with the specified ID.
Syntax:
document.getElementById(id)
The html dom defines multiple methods for searching elements. Besides getElementById (), getElementsByName () and getElementsByTagName ().
However, if you need to find a specific element in the document, the most effective method is getElementById ().
When operating on a specific element of a document, it is best to give the element an id attribute to specify a unique name (in the document) for it, then you can use this ID to find the desired element.
Example:
document.getElementById("myHeader") alert(x.innerHTML) }</script>
Example: getElementById () is an important method. It is very common in DOM programming. The following defines a tool function, so that you can use the getElementById () method with a short name:
function id(x) { if (typeof x == "string") return document.getElementById(x)
; return x; }
The above function accepts element IDS as their parameters. For each such parameter, you only need to write x = id (x) before use.