Creating button object
The button object can be created using JavaScript. The document.createElement() method is used to create <button> element. After creating a button object use the appendChild() method to append the particular element (such as div) to display it.
Example 1: In this example, we will use an HTML DOM button Object in an HTML document.
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 create a button.</ p > < button onclick = "Geeks()" > Press me! </ button > < br >< br > < div id = "GFG" ></ div > <!-- script to create new button --> < script > function Geeks() { let myDiv = document.getElementById("GFG"); // creating button element let button = document.createElement('BUTTON'); // creating text to be //displayed on button let text = document.createTextNode("Button"); // appending text to button button.appendChild(text); // appending button to div myDiv.appendChild(button);; } </ 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.