Javascript DOM HTML Input Number readOnly Property set

Introduction

Set a number field to read-only:

document.getElementById("myNumber").readOnly = true;

Click the button to set the number field to read-only.

View in separate window

<!DOCTYPE html>
<html>
<body>

<input type="number" id="myNumber" value="2">
<button onclick="myFunction()">Test</button>
<p id="demo"></p>

<script>
function myFunction() {/*w ww.j ava  2  s.co m*/
  document.getElementById("myNumber").readOnly = true;
}
</script>

</body>
</html>

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

A read-only field cannot be modified.

This property mirrors the HTML readonly attribute.

The readOnly property accepts and returns a boolean type value.

Value Description
true The number field is read-only
false Default. The number field is not read-only

The readOnly property returns true if the number field is read-only, otherwise it returns false.




PreviousNext

Related