Javascript Reference - HTML DOM Input Datetime readOnly Property








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

Browser Support

readOnly No No No No No

Syntax

Return the readOnly property.

var v = datetimeObject.readOnly

Set the readOnly property.

datetimeObject.readOnly=true|false

Property Values

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




Return Value

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

Example

The following code shows how to get if a datetime field is read-only.


<!DOCTYPE html>
<html>
<body>
<!-- ww w  .  j a v a 2 s.c om-->
Birthday: <input type="datetime" id="myDatetime" readonly>
<button onclick="myFunction()">test</button>
<p id="demo"></p>

<script>
function myFunction() {
    var x = document.getElementById("myDatetime").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 datetime field to read-only.


<!DOCTYPE html>
<html>
<body>
<!--from ww  w  .j  a v  a  2 s.  c om-->
Birthday: <input type="datetime" id="myDatetime">
<button onclick="myFunction()">test</button>
<p id="demo"></p>

<script>
function myFunction() {
    document.getElementById("myDatetime").readOnly = true;
}
</script>

</body>
</html>

The code above is rendered as follows: