over 9 years ago
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();
- }
- });
<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)