i2tutorials

Javascript Focus

Javascript Focus

 

The JavaScript focus method is used to give focus to an HTML element. It sets the element as the active(focus) element in the current document. It can be applied to one HTML element at a single time in a current or present document. The element can either be a button or a text field etc. It is supported by all browsers.

 

Syntax:

 

HTMLElementObject.focus()

 

Parameters: Not accept any parameters.
Return Value: Does not return any value.

 

EXAMPLE:

 

<html>

<head> 
      <script type="text/javascript"> 
              function myFunction() { 
                      document.getElementById("focus").focus(); 
              } 
      </script> 
</head>

<body> 
      <form action="#"> 
             <br> 
             <br> 
             <label> 
                    Hover me: </label> 
             <input type="text" onmousemove=myFunction() id="focus"> 

             <!-- onmousemove is an event which occurs when someone hovers the mouse on that particular element and calls 
the function of javascript --> 
             <br> 
             <br> 
             <label>Without Focus: </label> 
             <input type="text"> 
             <br> 
             <br> 
             <input type="button" value="submit"> 
      </form> 
</body> 
</html>

 

OUTPUT:

 

Javascript Focus

 

Exit mobile version