The Input Button object represents an HTML <input> element with type="button".
Property | Description |
---|---|
autofocus | Sets or gets whether a button can automatically get focus when the page loads |
defaultValue | Sets or gets the default value of a button |
disabled | Enable or disable a button |
form | Get a reference to the form that contains the button |
name | Sets or gets the value of the button name |
type | Get the element type for a button |
value | Sets or gets the button value attribute |
The Input Button object supports the standard properties and events.
We can access an <input> element with type="button" by using getElementById().
<!DOCTYPE html>
<html>
<body>
<input type="button" id="myBtn" onclick="myFunction()" value="test">
<p id="demo"></p>
<!--from w w w. j av a 2 s. c o m-->
<script>
function myFunction() {
var x = document.getElementById("myBtn").value;
document.getElementById("demo").innerHTML = x;
}
</script>
</body>
</html>
The code above is rendered as follows:
We can create an <input> element with type="button" by using the document.createElement() method.
<!DOCTYPE html>
<html>
<body>
<button onclick="myFunction()">test</button>
<script>
function myFunction() {<!-- w w w. j a v a2s . c o m-->
var x = document.createElement("INPUT");
x.setAttribute("type", "button");
x.setAttribute("value", "Click me");
document.body.appendChild(x);
}
</script>
</body>
</html>
The code above is rendered as follows: