JQuery | Set focus on a form input text field on page load
The task is to set the focus on form input text field using JQuery. To do so, Here are a few of the most used techniques discussed.
First focus() method need to be discussed.
-
jQuery focus() Method:
The focus event happens when an element gets focus (Either selected by a mouse click or by “tab-navigating” to it).
This method triggers the focus event, or adds a function to run when the focus event occurs.
Syntax:-
Trigger the focus event for selected elements:
$(selector).focus()
-
Attach a function to the focus event:
$(selector).focus(function)
Parameters:
- function: This parameter is optional. It specifies the function to execute when the focus event occurs.
-
Trigger the focus event for selected elements:
Example 1: In this example the form input text field gets the focus as page loads by using focus() method . Here the input element is selected by input keyword in JQuery selector.
<!DOCTYPE HTML> <html> <head> <title> JQuery | Set focus on a form input text field on page load. </title> <script src= </script> </head> <body style="text-align:center;" id="body"> <h1 style="color:green;"> GeeksForGeeks </h1> <p id="GFG_UP" style="font-size: 15px; font-weight: bold;"> This input box gets the focus as the page loads. </p> <form id="form"> Input : <input type="text" name="input_field" /> </form> <br> <p id="GFG_DOWN" style="color:green; font-size: 20px; font-weight: bold;"> </p> <script> $("input:text").focus(); </script> </body> </html> |
Output:
-
Before page loads:
-
After page loads:
Example 2: In this example the form input text field gets the focus as page loads by using focus() method . Here the input element is selected by id of form element and its own id in JQuery selector.
<!DOCTYPE HTML> <html> <head> <title> JQuery | Set focus on a form input text field on page load. </title> <script src= </script> </head> <body style="text-align:center;" id="body"> <h1 style="color:green;"> GeeksForGeeks </h1> <p id="GFG_UP" style="font-size: 15px; font-weight: bold;"> This input box gets the focus as the page loads. </p> <form id="form"> Input : <input id="form_input" type="text" name="input_field" /> </form> <br> <p id="GFG_DOWN" style="color:green; font-size: 20px; font-weight: bold;"> </p> <script> $("#form #form_input").focus(); </script> </body> </html> |
Output:
-
Before page loads:
-
After page loads:
Recommended Posts:
- JQuery | Set the value of an input text field
- How to get the value of text input field using JavaScript?
- How to select all on focus in input using JQuery?
- How to disable browser Autocomplete on web form field/input tag?
- How to detect value change on hidden input field in JQuery ?
- HTML | DOM Input Text form Property
- How to disable scroll to change number in <input type="number"> field using JavaScript/jQuery?
- How to enable/disable all input controls inside a form element using jQuery ?
- How to create fade-in effect on page load using CSS ?
- How to execute AngularJS controller function on page load ?
- How to select all text in HTML text input when clicked using JavaScript?
- jQuery | :focus Selector
- jQuery | focus() with Examples
- Set the focus to HTML form element using JavaScript
- jQuery | load() with Examples
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.



