I have a searchbox on my site that. Currently, users must click the submit button next to the box to search via jquery's post. I would like to let users also press enter to search. How can i do this?
JQUERY:
$('document').ready(function(){
$('#searchButton').click(function(){
var search = $('#usersSearch').val();
$.post('../searchusers.php',{search: search},function(response){
$('#userSearchResultsTable').html(response);
});
});
});
HTML:
<input type='text' id='usersSearch' /><input type='button' id='searchButton' value='search' />
You call both event listeners using
.on()
then use aif
inside the function:Take a look at the
keypress
function.I believe the
enter
key is13
so you would want something like:Use
keypress
event onusersSearch
textbox and look for Enter button. If enter button is pressed then trigger the search button click event which will do the rest of work. Try this.Demo
Something like this will work