The value
property sets or gets the
value of the value attribute of a button.
value |
Yes | Yes | Yes | Yes | Yes |
Return the value property:
var v = buttonObject.value
Set the value property:
buttonObject.value=text
Value | Description |
---|---|
text | The initial value of the button |
A String, representing the value that is associated with the button.
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:
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: