Document getElementById() Method - Javascript DOM

Javascript examples for DOM:Document getElementById

Description

The getElementById() method returns the element by ID attribute value.

Parameter Values

Parameter Type Description
elementID String Required. The ID attribute's value of the element you want to get

Technical Details

Return Value:

An Element Object, representing an element with the specified ID. Returns null if no elements with the specified ID exists

The following code shows how to Get the element with the specified ID:

Demo Code

ResultView the demo in separate window

<!DOCTYPE html>
<html>
<body>

<p id="demo">Click the button to change the color of this paragraph.</p>

<button onclick="myFunction()">Test</button>

<script>
function myFunction() {//from www .j a  v a 2s.  c  om
    var x = document.getElementById("demo");
    x.style.color = "red";
}
</script>

</body>
</html>

Related Tutorials