Javascript examples for DOM:Document getElementById
The getElementById() method returns the element by ID attribute value.
Parameter | Type | Description |
---|---|---|
elementID | String | Required. The ID attribute's value of the element you want to get |
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:
<!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>