Accessing button object
Access the button object by using the getElementById() method. Put the id of the button element in the getElementById() to access it.
Example 2: In this example, we will use the button object by using the getElementById() method
html
<!DOCTYPE html> < html > < head > < title > DOM Button Object </ title > </ head > < body style = "text-align: center;" > < h1 style = "color:green;" > w3wiki </ h1 > < h2 > DOM Button Property </ h2 > < p > Click the button to change the text inside it. </ p > < button type = "button" id = "btn" onclick = "geek()" > Try it </ button > < script > function geek() { // Accessing the button element // by using id attribute let doc = document.getElementById("btn"); // Changing the text content doc.textContent = "Click me!"; } </ script > </ body > </ html > |
Output:
HTML DOM button Object
The button object in HTML is used to represent a <button> element. The getElementById() method is used to get the button object.