Javascript Reference - HTML DOM Input Button value Property








The value attribute from button element defines the text shown on the button.

The value property sets or gets the value of the value attribute of an input button.

Browser Support

value Yes Yes Yes Yes Yes

Syntax

Return the value property.

var v = buttonObject.value 

Set the value property.

buttonObject.value=text




Property Values

Value Description
text The text displayed on the button

Return Value

A String type value representing the text displayed on the input button.

Example

The following code shows how to get the text displayed on a button.


<!DOCTYPE html>
<html>
<body>
<input type="button" onclick="myFunction()" id="myBtn" value="test">
<p id="demo"></p>
<script>
function myFunction() {<!--from w  ww  .  ja  v a 2 s . com-->
    var x = document.getElementById("myBtn").value;
    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 text displayed on a button.


<!DOCTYPE html>
<html>
<body>
<input type="button" id="myBtn" value="my button">
<button onclick="myFunction()">test</button>
<script>
function myFunction() {<!--from   w  w  w  .j  av  a2  s. co m-->
    document.getElementById("myBtn").value = "a button";
}
</script>

</body>
</html>

The code above is rendered as follows: