HTML DOM Input Search disabled Property


The HTML DOM Input Search disabled property is used for setting or returning whether the search field should be disabled or not. It uses boolean values with true representing the element should be disabled and false otherwise. The disabled property is set to false by default. However, the disabled element is greyed out by default and is unclickable.

Syntax

Following is the syntax for −

Setting the disabled property −

searchObject.autofocus = true|false

Here, true=search field is disabled and false=the search field is not disabled. It is false by default.

Example

Let us look at an example for the Input Search disabled property −

Live Demo

<!DOCTYPE html>
<html>
<body>
<h1>Input search disabled Property</h1>
<form>
FRUITS: <input type="search" id="SEARCH1" name="fruits">
<form>
<p>Disable the above search field by clicking on the DISABLE button</p>
<button type="button" onclick="disableSearch()">DISABLE</button>
<p id="Sample"></p>
<script>
   function disableSearch() {
      document.getElementById("SEARCH1").disabled=true;
      document.getElementById("Sample").innerHTML = "The search field is now disabled" ;
   }
</script>
</body>
</html>

Output

This will produce the following output −

On clicking the DISABLE button −

In the above example −

We have first created an <input> element with type=”search”, id=”SEARCH1”, and name=”fruits”. The search field is inside a form −

<form>
FRUITS: <input type="search" id="SEARCH1" name="fruits">
<form>

We then create a button CHANGE that will execute the disableSearch() method when clicked by the user −

<button type="button" onclick="disableSearch()">DISABLE</button>

The disableSearch() method gets the input element with type search using the getElementById() method and sets its disabled property to true. This makes the search field unclickable anymore and the user can no longer interact with it. It has turned grey now −

function disableSearch() {
   document.getElementById("SEARCH1").disabled=true;
   document.getElementById("Sample").innerHTML = "The search field is now disabled" ;
}

Updated on: 19-Feb-2021

174 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements