Javascript Reference - HTML DOM Button type Property








The type property sets or gets the type of a button.

Browser Support

type Yes Yes Yes Yes Yes

Syntax

Return the type property:

var v = buttonObject.type

Set the type property:

buttonObject.type='submit|button|reset'

Property Values

Value Description
submit The button is a submit button. This is default for all browsers, except Internet Explorer.
button The button is a clickable button. This is default for IE.
reset The button is a reset button.




Return Value

A String representing the button type.

Example

The following code shows how to get the type of a button


<!DOCTYPE html>
<html>
<body>
<button type="button" id="myBtn" onclick="myFunction()">test</button>
<p id="demo"></p>
<!--  www .j  a  v a 2  s  .c o  m-->
<script>
function myFunction() {
    var x = document.getElementById("myBtn").type;
    document.getElementById("demo").innerHTML = x;
}
</script>

</body>
</html>

The code above is rendered as follows:





Example 2

The following code shows how to change the type of a button to "submit".


<!DOCTYPE html>
<html>
<body>
<!--from w ww  .  j a v a 2  s. com-->
<form id="myForm" action="form_action.asp">
First name: <input type="text" name="fname"><br>
Last name: <input type="text" name="lname"><br>
<button id="myBtn" type="button" value="Submit">Submit</button>
</form>
<p id="demo"></p>

<button onclick="myFunction()">test</button>

<script>
function myFunction() {
    var x = document.getElementById("myBtn").type = "submit";
    document.getElementById("demo").innerHTML = "changed";
}
</script>

</body>
</html>

The code above is rendered as follows:

Example 3

The following code shows how to change the type of a button to "reset".


<!DOCTYPE html>
<html>
<body>
<!-- www. j  a  va  2  s.  c o m-->
<form id="myForm" action="form_action.asp">
First name: <input type="text" name="fname"><br>
Last name: <input type="text" name="lname"><br>
<button id="myBtn" type="button" value="Reset">Reset</button>
</form>
<p id="demo"></p>

<button onclick="myFunction()">test</button>

<script>
function myFunction() {
    var x = document.getElementById("myBtn").type = "reset";
    document.getElementById("demo").innerHTML = "changed";
}
</script>

</body>
</html>

The code above is rendered as follows: