Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

JQuery Event for when user presses enter?

What's the simplest way to have a function called whenever the user hits enter after typing in a textbox?

like image 650
Ali Avatar asked Feb 28 '26 04:02

Ali


2 Answers

You'll need to listen for the keypress event. It's probably easiest to do this with delegate:

$(document.body).delegate('input:text', 'keypress', function(e) {
    if (e.which === 13) { // if is enter
        e.preventDefault(); // don't submit form

        // do what you want here
    }
});
like image 80
lonesomeday Avatar answered Mar 01 '26 16:03

lonesomeday


<textarea id="text"></textarea>

$('#text').keydown(function(e){
    if (e.keyCode == 13) {
        alert('Enter was pressed.');
    }
});

http://jsfiddle.net/dNfC2/

like image 33
Jared Farrish Avatar answered Mar 01 '26 18:03

Jared Farrish



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!