Home
HTML
HTML id attribute
Tryit: Get element by ID
Run ❯
Get your
own
website
Result Size:
625 x 534
×
Change Orientation
Save Code
Change Theme, Dark/Light
Go to Spaces
<!DOCTYPE html> <html> <body> <h2>Using The id Attribute in JavaScript</h2> <p>JavaScript can access an element with a specified id by using the getElementById() method:</p> <h1 id="myHeader">Hello World!</h1> <button onclick="displayResult()">Change text</button> <script> function displayResult() { document.getElementById("myHeader").innerHTML = "Have a nice day!"; } </script> </body> </html>