Skip to content Skip to sidebar Skip to footer

Running A Function When Enter Is Pressed In A Text Box

This is my input form: I basically want a Javascript function to execute when the person presses enter in the text box. I'm sorry if this is

Solution 1:

$('#word').on('keyup', function(e){
  if(e.which === 13){
    // User pressed enter
  }
});

Testing the event which property will tell you what key the user has pressed. which is normalized by jQuery, so you won't have any cross-browser issues.

Solution 2:

keyup() is your answer:

$('#word').keyup(function(e) {
    if(e.which == 13) {
        // Do your stuff
    }
});

e.which returns the scan/character code of the pressed key. 13 is for ENTER.

You can find other codes here.

Post a Comment for "Running A Function When Enter Is Pressed In A Text Box"