Trigger an event on `click` and `enter`

JqueryJquery Selectors

Jquery Problem Overview


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' />

Jquery Solutions


Solution 1 - Jquery

Use keypress event on usersSearch 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.

$('document').ready(function(){
    $('#searchButton').click(function(){
        var search = $('#usersSearch').val();
        $.post('../searchusers.php',{search: search},function(response){
            $('#userSearchResultsTable').html(response);
        });
    })
    $('#usersSearch').keypress(function(e){
        if(e.which == 13){//Enter key pressed
            $('#searchButton').click();//Trigger search button click event
        }
    });
 
});

Demo

Solution 2 - Jquery

You call both event listeners using .on() then use a if inside the function:

$(function(){
  $('#searchButton').on('keypress click', function(e){
    var search = $('#usersSearch').val();
    if (e.which === 13 || e.type === 'click') {
      $.post('../searchusers.php', {search: search}, function (response) {
        $('#userSearchResultsTable').html(response);
      });
    }
  });
});

Solution 3 - Jquery

Something like this will work

$('#usersSearch').keypress(function(ev){
    if (ev.which === 13)
        $('#searchButton').click();
});

Solution 4 - Jquery

$('#form').keydown(function(e){
    if (e.keyCode === 13) { // If Enter key pressed
        $(this).trigger('submit');
    }
});

Solution 5 - Jquery

you can use below event of keypress on document load.

 $(document).keypress(function(e) {
            if(e.which == 13) {
               yourfunction();
            }
        });

Thanks

Solution 6 - Jquery

Take a look at the keypress function.

I believe the enter key is 13 so you would want something like:

$('#searchButton').keypress(function(e){
    if(e.which == 13){  //Enter is key 13
        //Do something
    }
});

Solution 7 - Jquery

$('#usersSearch').keyup(function() { // handle keyup event on search input field

    var key = e.which || e.keyCode;  // store browser agnostic keycode

    if(key == 13) 
        $(this).closest('form').submit(); // submit parent form
}

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionkirbyView Question on Stackoverflow
Solution 1 - JqueryShankarSangoliView Answer on Stackoverflow
Solution 2 - JqueryGustavo RodriguesView Answer on Stackoverflow
Solution 3 - JqueryjopkeView Answer on Stackoverflow
Solution 4 - JqueryelclanrsView Answer on Stackoverflow
Solution 5 - JqueryAsif GhanchiView Answer on Stackoverflow
Solution 6 - Jquerynolt2232View Answer on Stackoverflow
Solution 7 - JqueryAlexView Answer on Stackoverflow