Join the social network of Tech Nerds, increase skill rank, get work, manage projects...
 
  • How to focus a text box on button click in jQuery?

    • 0
    • 1
    • 0
    • 1
    • 0
    • 0
    • 0
    • 0
    • 319
    Comment on it

    Sometimes we need to focus a textbox on button click based on some condition. We can do this by using focus().

    Example- Suppose I want to focus search-box on button click if the search-box is empty, then we can do this as below:

    <input id="search-box" type="text" placeholder="search city...."/>
    <input type="button" id="search-submit" value="Search" />
    
    
    $("#search-submit").on("click",function(e){
            if($("#search-box").val() == "")
            {
                    $("#search-box").focus();
            }
        });
    

    Hope this will help you :)

 0 Comment(s)

Sign In
                           OR                           
                           OR                           
Register

Sign up using

                           OR                           
Forgot Password
Fill out the form below and instructions to reset your password will be emailed to you:
Reset Password
Fill out the form below and reset your password: