Javascript Reference - HTML DOM Input Number value Property








The value property sets or gets the value attribute of a number field.

Browser Support

value Yes 10.0 Yes Yes Yes

Syntax

Return the value property.

var v = numberObject.value

Set the value property.

numberObject.value=number

Property Values

Value Description
number Set the value of the number field




Return Value

A String type value representing a number.

Example

The following code shows how to use .Get the number of a number field.


<!DOCTYPE html>
<html>
<body>
Number: <input type="number" id="myNumber" value="2">
<button onclick="myFunction()">test</button>
<p id="demo"></p>
<script>
function myFunction() {<!--   ww w  . j  a  v  a2 s  . c om-->
    var x = document.getElementById("myNumber").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 number of a number field.


<!DOCTYPE html>
<html>
<body>
<!--from  ww  w  . j a  v a  2s .  c  om-->
<input type="number" id="myNumber" value="2">
<button onclick="myFunction()">test</button>
<p id="demo"></p>
<script>
function myFunction() {
    document.getElementById("myNumber").value = "16";
}
</script>

</body>
</html>

The code above is rendered as follows: