Javascript Reference - HTML DOM Input Search readOnly Property








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

Browser Support

readOnly Yes Yes Yes Yes Yes

Syntax

Return the readOnly property.

var v = searchObject.readOnly

Set the readOnly property.

searchObject.readOnly=true|false

Property Values

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




Return Value

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

Example

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


<!DOCTYPE html>
<html>
<body>
<!--from w w  w. j a  v  a  2  s  .  c o  m-->
Search: <input type="search" id="mySearch" readonly>
<button onclick="myFunction()">test</button>
<p id="demo"></p>
<script>
function myFunction() {
    var x = document.getElementById("mySearch").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 search field to read-only.


<!DOCTYPE html>
<html>
<body>
<!--from www. java2 s .  c om-->
Search: <input type="search" id="mySearch">
<button onclick="myFunction()">test</button>
<p id="demo"></p>

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

</body>
</html>

The code above is rendered as follows: