Wednesday, February 8, 2017

Focus on a textbox using jQuery

You may want to auto focus on a textbox in cases like when you have a search textbox dropdown when clicking on a search button.

The jQuery method .focus() can help with this. However in order for it to work you may need to add in a small delay in order for the browser to pick it up.

// Click event
$('#btnSearch').click(function(){

 // focus on search bar after delay
 setTimeout(function () {
     $('#txtSearch').focus();
 }, 500);

});

No comments:

Post a Comment