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
    • 379
    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:

    1. <input id="search-box" type="text" placeholder="search city...."/>
    2. <input type="button" id="search-submit" value="Search" />
    3.  
    4.  
    5. $("#search-submit").on("click",function(e){
    6. if($("#search-box").val() == "")
    7. {
    8. $("#search-box").focus();
    9. }
    10. });

    Hope this will help you :)

 0 Comment(s)

Sign In
                           OR                           
                           OR                           
Register

Sign up using

                           OR                           
Forgot Password
Reset Password
Fill out the form below and reset your password: