77

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

7 Answers 7

117

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

2
  • 1
    Better yet: use the keyup event instead to ensure your function is only triggered once. Thanks to @StevePaulo for his comment in this answer.
    – steps
    Commented Nov 7, 2014 at 11:57
  • 2
    A downside of using keyup that I just discovered: You won't be able to prevent the default action, because the keyup event is fired only after the default action for the key has been executed (see here).
    – steps
    Commented Nov 7, 2014 at 15:46
75

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);
      });
    }
  });
});
5
  • 10
    Why does this answer does not get more votes? I mean this is lesser code blocks (like not to add another event to trigger a click). Any downside of this?
    – Nin-ya
    Commented Apr 10, 2015 at 5:31
  • Truly said, Nin-ya & sagarthapa. Commented Jan 28, 2016 at 5:41
  • 4
    You need to capture the keypress from the search field, and the click from the search button. Those are two separate DOM entities, and so this code won't work.
    – Moolie
    Commented Apr 14, 2017 at 15:26
  • It worked for me but I had to add e.preventDefault() as a first line to prevent double execution
    – Novasol
    Commented May 30, 2018 at 9:15
  • TIL: I can listen for multiple events with on(), Had no idea you could do that! Commented May 29, 2019 at 21:09
6

Something like this will work

$('#usersSearch').keypress(function(ev){
    if (ev.which === 13)
        $('#searchButton').click();
});
2
  • but how can i use both, press search or press enter?
    – kirby
    Commented Feb 5, 2012 at 3:23
  • 1
    You put the code I added in addition to the code you already have. Then both will work.
    – jopke
    Commented Feb 5, 2012 at 3:23
3
$('#form').keydown(function(e){
    if (e.keyCode === 13) { // If Enter key pressed
        $(this).trigger('submit');
    }
});
0
2

you can use below event of keypress on document load.

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

Thanks

1
$('#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
}
1

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
    }
});

Not the answer you're looking for? Browse other questions tagged or ask your own question.