Javascript Reference - HTML DOM Input Number readOnly Property








The readOnly property sets or gets whether a number field is read-only.

Browser Support

readOnly Yes 10.0 Yes Yes Yes

Syntax

Return the readOnly property.

var v = numberObject.readOnly

Set the readOnly property.

numberObject.readOnly=true|false

Property Values

Value Description
true|false Set whether a number field should be read-only
  • true - The number field is read-only
  • false - Default. The number field is not read-only




Return Value

A Boolean type value, true if the number field is read-only, otherwise false.

Example

The following code shows how to check if a number field is read-only.


<!DOCTYPE html>
<html>
<body>
<!--from  w  w  w. j  a  v a  2  s.  c  om-->
<input type="number" id="myNumber" value="2" readonly>
<button onclick="myFunction()">test</button>
<p id="demo"></p>

<script>
function myFunction() {
    var x = document.getElementById("myNumber").readOnly;
    document.getElementById("demo").innerHTML = x;
}
</script>

</body>
</html>

The code above is rendered as follows:





Example 2

The following code shows how to set a number field to read-only.


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

</body>
</html>

The code above is rendered as follows: