Skip to content
Related Articles

Related Articles

How to set cursor in search form by clicking on button in jQuery ?

View Discussion
Improve Article
Save Article
Like Article
  • Last Updated : 10 Mar, 2022

Given a search form, we need to set the cursor in the search form on clicking a button using jQuery. This can be done by using the focus() method and click() method.

We will create a web page with a textbox and a button. We will add the functionality using jQuery such that on clicking the button, the cursor is set to the textbox. Now let us discuss some methods that we will use in the implementation.

click() Method: The click() method in jQuery starts the click event or runs the function passed to it when a click event is triggered.



Parameter: It accepts an optional parameter “function”, which will run when a click event occurs.

Return Value: It returns the selected element with the specified function to perform.

focus() Method: The focus() method triggers the focus event or runs the function passed to it when a focus event is triggered. The focus() method can be used to focus a specific element by using the selector.



Parameter: It accepts an optional parameter “function”  which will run when a focus event occurs.

jQuery CDN Links:

<link rel=”stylesheet” href=””>
<script src=””></script>


  • Add the jQuery CDN to the script or download them to your local machine.
  • Create a textbox and button using bootstrap so that it would be good-looking.
  • Open script tag and write click handler function on clicking the button using the click() function.
  • In the click handler, use the focus() method to set the cursor to the textbox by using the selector.



<!DOCTYPE html>
<html lang="en">
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content=
        "width=device-width, initial-scale=1.0">
    <link rel="stylesheet" href=
    <script src=
        body {
            margin-left: 10px;
            margin-right: 10px;
    <h2 style="color:green">GeeksforGeeks</h2>
    <strong>Set cursor in search form</strong>
    <br />
    <form class="form-inline mt-4">
        <input type="text" class="form-control mx-2"
            id="textInput" placeholder="Focus Me...">
        <button id="focusButton" type="button"
            class="btn btn-secondary">
            Click Here To Focus
        $(document).ready(function () {
            $('#focusButton').click(function () {


My Personal Notes arrow_drop_up
Recommended Articles
Page :

Start Your Coding Journey Now!