The Input Number object represents an HTML <input> element with type="number".
Property | Description |
---|---|
autocomplete | Sets or gets the autocomplete attribute of a number field |
autofocus | Sets or gets whether a number field can auto focus when the page loads |
defaultValue | Sets or gets the default value of a number field |
disabled | Disable or enable a number field |
form | Get the form that contains the number field |
list | Get the datalist that contains the number field |
max | Sets or gets the max attribute of a number field |
min | Sets or gets the min attribute of a number field |
name | Sets or gets the name attribute of a number field |
placeholder | Sets or gets the placeholder attribute of a number field |
readOnly | Sets or gets whether the number field is read-only |
required | Sets or gets whether the number field must be filled before submitting a form |
step | Sets or gets the step attribute of a number field |
type | Get the type of the number field |
value | Sets or gets the value attribute of a number field |
The Input Number object supports the standard properties and events.
We can access an <input> element with type="number" by using getElementById().
<!DOCTYPE html>
<html>
<body>
<input type="number" id="myNumber" value="2">
<button onclick="myFunction()">test</button>
<p id="demo"></p>
<script>
function myFunction() {<!-- ww w. j a v a 2 s . c o m-->
var x = document.getElementById("myNumber").value;
document.getElementById("demo").innerHTML = x;
}
</script>
</body>
</html>
The code above is rendered as follows:
We can create an <input> element with type="number" by using the document.createElement() method.
<!DOCTYPE html>
<html>
<body>
<button onclick="myFunction()">test</button>
<script>
function myFunction() {<!-- ww w . j a va 2s .c o m-->
var x = document.createElement("INPUT");
x.setAttribute("type", "number");
x.setAttribute("value", "12345");
document.body.appendChild(x);
}
</script>
</body>
</html>
The code above is rendered as follows: