The DOM Input Type Button Object is used to represent the HTML <input> element with type=”button”. The Input Type Button element is accessed by getElementById().
Syntax:
document.getElementById("ID");
Where “id” is the ID assigned to the “input” tag.
Example 1: In this example, we will see the use of the DOM Input Button Object
HTML
<!DOCTYPE html>
< html >
< head >
< style >
h1 {
color: green;
}
</ style >
</ head >
< body >
< h1 >GeeksForGeeks</ h1 >
< h2 > DOM Input Button Object </ h2 >
< input type = "button" id = "GFG" onclick = "myGeeks()" value = "Submit" >
< p id = "sudo" ></ p >
< script >
function myGeeks() {
// accessing 'button' id.
let g = document.getElementById("GFG").value;
document.getElementById("sudo").innerHTML =
"Value od input type button is:" + g;
}
</ script >
</ body >
</ html >
|
Output:
Example 2: Input button Object can be created by using the document.createElement method.
HTML
<!DOCTYPE html>
< html >
< head >
< style >
h1 {
color: green;
}
#GFG {
margin-left: 45%;
margin-top: 30px;
}
</ style >
</ head >
< body >
< h1 >GeeksForGeeks</ h1 >
< h2 > DOM Input Button Object </ h2 >
< h3 style = "color:green;" >
Click the Button to Create the Button</ h3 >
< button onclick = "myGeeks()" >Submit</ button >
< script >
function myGeeks() {
let g = document.createElement("INPUT");
g.setAttribute("type", "button");
g.setAttribute("value", "Click me");
g.setAttribute("id", "GFG");
document.body.appendChild(g);
}
</ script >
</ body >
</ html >
|
Output:
Supported Browsers: The browser supported by DOM Input button Object are listed below:
- Google Chrome 1 and above
- Firefox 1 and above
- Opera
- Safari 1 and above
- Edge 12 and above
Whether you're preparing for your first job interview or aiming to upskill in this ever-evolving tech landscape,
GeeksforGeeks Courses are your key to success. We provide top-quality content at affordable prices, all geared towards accelerating your growth in a time-bound manner. Join the millions we've already empowered, and we're here to do the same for you. Don't miss out -
check it out now!
Last Updated :
14 Jun, 2023
Like Article
Save Article