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' />
Amar Palsapure
  • 9,590
  • 1
  • 27
  • 46
kirby
  • 3,981
  • 14
  • 41
  • 54

7 Answers7

116

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

ShankarSangoli
  • 69,612
  • 13
  • 93
  • 124
  • 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](http://stackoverflow.com/a/155263/461544). – steps Nov 07 '14 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](http://www.quirksmode.org/dom/events/keys.html)). – steps Nov 07 '14 at 15:46
74

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);
      });
    }
  });
});
Gustavo Rodrigues
  • 3,517
  • 1
  • 25
  • 21
6

Something like this will work

$('#usersSearch').keypress(function(ev){
    if (ev.which === 13)
        $('#searchButton').click();
});
Diogo Cid
  • 3,764
  • 1
  • 20
  • 25
jopke
  • 1,186
  • 6
  • 18
3
$('#form').keydown(function(e){
    if (e.keyCode === 13) { // If Enter key pressed
        $(this).trigger('submit');
    }
});
elclanrs
  • 92,861
  • 21
  • 134
  • 171
2

you can use below event of keypress on document load.

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

Thanks

Asif Ghanchi
  • 236
  • 3
  • 11
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
    }
});
atiquratik
  • 1,296
  • 3
  • 27
  • 34
nolt2232
  • 2,594
  • 1
  • 22
  • 33
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
}
Alex
  • 34,899
  • 5
  • 77
  • 90