javascript getElementById

getElementById

The document.getElementById(id) is a method of the document object that is used to get an element by its element id. It is the easiest way to find an HTML element in the DOM.

Syntax:

document.getElementById(id)

Example:

<!DOCTYPE html>
<html>
<body>
<h1 id="DOM1">"Get Element by ID!"</h1>
<p id="DOM2"></p>
<script>
var myElement = document.getElementById("DOM1");
document.getElementById("DOM2").innerHTML =
"The text from DOM1 is " + myElement.innerHTML;
</script>
</body>
</html>