Javascript Reference - HTML DOM Button value Property








The value property sets or gets the value of the value attribute of a 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 initial value of the button




Return Value

A String, representing the value that is associated with the button.

Example

The following code shows how to get the value of the value attribute of a button.


<!DOCTYPE html>
<html>
<body>
<button id="myBtn" value="myvalue" onclick="myFunction()">test</button>
<!-- w ww.  j  a  v a  2  s .  com-->
<p id="demo"></p>

<script>
function myFunction() {
    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 value of the value attribute of a button.


<!DOCTYPE html>
<html>
<body>
<button id="myBtn" name="myname" value="myvalue" 
   onclick="console.log(this.value)">My Button</button>
<button onclick="myFunction()">test</button>
<script>
function myFunction() {<!--from www  .j a v  a  2 s  . co m-->
    document.getElementById("myBtn").value = "newButtonValue";
}
</script>

</body>
</html>

The code above is rendered as follows: