Javascript DOM HTML Document getElementById() Method

Introduction

Get the element with the specified ID:

document.getElementById("demo");

View in separate window

<!DOCTYPE html>
<html>
<body>

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

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

<script>
function myFunction() {/*from  www. jav a2 s .c  om*/
  document.getElementById("demo").innerHTML = "Hello World";
}
</script>

</body>
</html>

The getElementById() method returns the element by its ID.

It returns null if no elements with the specified ID exists.

An ID should be unique within a page.

If more than one element with the specified ID exists, the getElementById() method returns the first element in the source code.

document.getElementById(elementID)

Parameter Values

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

The getElementById() method returns an Element Object representing an element with the specified ID.

The getElementById() method returns null if no elements with the specified ID exists.




PreviousNext

Related