HTML DOM Select autofocus Property


The HTML DOM select autofocus property returns and modify whether the drop-down list should get focused or not when page load.

Syntax

Following is the syntax −

1. Returning autofocus

object.autofocus

2. Modifying autofocus

object.autofocus = true | false

Example

Let us see an example of HTML DOM select autofocus property −

 Live Demo

<!DOCTYPE html>
<html>
<head>
<title>HTML DOM autofocus property</title>
<style>
   body{
      text-align:center;
      color:#fff;
      background: radial-gradient( circle farthest-corner at 23.1% 64.6%, rgba(129,125,254,1) 0%, rgba(111,167,254,1) 90% ) no-repeat;
      height:100vh;
   }
   p{
      font-weight:700;
      font-size:1.1rem;
   }
   .drop-down{
      display:block;
      width:35%;
      border:none;
      font-weight:bold;
      padding:10px;
      margin:1rem auto;
      background-color:#ffffff7a;
      outline-color:black;
   }
   .btn{
      background:orange;
      border:none;
      height:2rem;
      border-radius:2px;
      width:35%;
      margin:2rem auto;
      display:block;
      color:#fff;
      font-weight:bold;
      outline:none;
      cursor:pointer;
   }
</style>
</head>
<body>
<h1>autofocus Property Example</h1>
<p>Hi, Select your favourite subject:</p>
<select class='drop-down' name="Drop Down List">
<option>Physics</option>
<option>Maths</option>
<option>Chemistry</option>
<option>English</option>
<option>Economics</option>
<option>Hindi</option>
<option>Biology</option>
</select>
<br>
<button onclick="disable()" class="btn">Disable Autofocus</button>
<script>
   function disable() {
      document.querySelector(".drop-down").autofocus = false;
   }
</script>
</body>
</html>

Output

This will produce the following output −

Click on “Disable Autofocus” button to disable autofocus on drop-down list.

raja
Published on 19-Jul-2019 12:13:19
Advertisements