Document getElementById() Method

Document Object Reference Document Object

Definition and Usage

The getElementById() method accesses the first element with the specified id.

Syntax

document.getElementById("id")

Parameter Description
id Required. The id of the element you want to access/manipulate

Browser Support

Internet Explorer Firefox Opera Google Chrome Safari

The getElementById() method is supported in all major browsers.


Example

Example

Alert innerHTML of an element with a specific ID:

<html>
<head>
<script>
function getValue()
  {
  var x=document.getElementById("myHeader");
  alert(x.innerHTML);
  }
</script>
</head>
<body>

<h1 id="myHeader" onclick="getValue()">Click me!</h1>

</body>
</html>

Try it yourself »

Document Object Reference Document Object

SHARE THIS PAGE

COLOR PICKER

colorpicker





Search w3schools.com:

Translate w3schools.com: