Detect the Enter key in a text input field

JavascriptJquery

Javascript Problem Overview


I'm trying to do a function if enter is pressed while on specific input.

What I'm I doing wrong?

$(document).keyup(function (e) {
	if ($(".input1").is(":focus") && (e.keyCode == 13)) {
		// Do something
	}
});

Is there a better way of doing this which would say, if enter pressed on .input1 do function?

Javascript Solutions


Solution 1 - Javascript

$(".input1").on('keyup', function (e) {
    if (e.key === 'Enter' || e.keyCode === 13) {
		// Do something
	}
});

// e.key is the modern way of detecting keys
// e.keyCode is deprecated (left here for for legacy browsers support)
// keyup is not compatible with Jquery select(), Keydown is.

Solution 2 - Javascript

event.key === "Enter"

More recent and much cleaner: use event.key. No more arbitrary number codes!

> NOTE: The old properties (.keyCode and .which) are Deprecated.

const node = document.getElementsByClassName("input1")[0];
node.addEventListener("keyup", function(event) {
    if (event.key === "Enter") {
        // Do work
    }
});

Modern style, with lambda and destructuring

node.addEventListener("keyup", ({key}) => {
    if (key === "Enter") {
        // Do work
    }
})

If you must use jQuery:

$(document).keyup(function(event) {
    if ($(".input1").is(":focus") && event.key == "Enter") {
        // Do work
    }
});

Mozilla Docs

Supported Browsers

Solution 3 - Javascript

$(document).keyup(function (e) {
    if ($(".input1:focus") && (e.keyCode === 13)) {
       alert('ya!')
    }
 });

Or just bind to the input itself

$('.input1').keyup(function (e) {
    if (e.keyCode === 13) {
       alert('ya!')
    }
  });

To figure out which keyCode you need, use the website http://keycode.info

Solution 4 - Javascript

Try this to detect the Enter key pressed in a textbox.

$(function(){

$(".input1").keyup(function (e) {
    if (e.which == 13) {
        // Enter key pressed
    }
 });

});

Solution 5 - Javascript

The best way I found is using keydown ( the keyup doesn't work well for me).

Note: I also disabled the form submit because usually when you like to do some actions when pressing Enter Key the only think you do not like is to submit the form :)

$('input').keydown( function( event ) {
	if ( event.which === 13 ) {
		// Do something
		// Disable sending the related form
		event.preventDefault();
		return false;
	}
});

Solution 6 - Javascript

It may be too late to answer this question. But the following code simply prevents the enter key. Just copy and paste should work.

		<script type="text/javascript"> 
		function stopRKey(evt) { 
		  var evt = (evt) ? evt : ((event) ? event : null); 
		  var node = (evt.target) ? evt.target : ((evt.srcElement) ? evt.srcElement : null); 
		  if ((evt.keyCode == 13) && (node.type=="text"))  {return false;} 
		} 

		document.onkeypress = stopRKey; 

		</script>

Solution 7 - Javascript

The solution that work for me is the following

$("#element").addEventListener("keyup", function(event) {
    if (event.key === "Enter") {
        // do something
    }
});

Solution 8 - Javascript

Try this to detect the Enter key pressed in a textbox.

$(document).on("keypress", "input", function(e){
    if(e.which == 13){
        alert("Enter key pressed");
    }
});

DEMO

Solution 9 - Javascript

 $(document).ready(function () {
        $(".input1").keyup(function (e) {
            if (e.keyCode == 13) {
                // Do something
            }
        });
    });

Solution 10 - Javascript

This code handled every input for me in the whole site. It checks for the ENTER KEY inside an INPUT field and doesn't stop on TEXTAREA or other places.

$(document).on("keydown", "input", function(e){
 if(e.which == 13){
  event.preventDefault();
  return false;
 }
});

Solution 11 - Javascript

Here is what I did for my angular project:

HTML:

<input
    class="form-control"
    [(ngModel)]="searchFirstName"
    (keyup)="keyUpEnter($event)"
/>

TypeScript:

keyUpEnter(event: KeyboardEvent) {
    if (event.key == 'Enter') {
        console.log(event);
    }
}

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
QuestionjQuerybeastView Question on Stackoverflow
Solution 1 - JavascriptJoseph SilberView Answer on Stackoverflow
Solution 2 - JavascriptGiboltView Answer on Stackoverflow
Solution 3 - JavascriptwesbosView Answer on Stackoverflow
Solution 4 - JavascriptShankarSangoliView Answer on Stackoverflow
Solution 5 - JavascriptRoy ShoaView Answer on Stackoverflow
Solution 6 - JavascriptIndra Kumar SView Answer on Stackoverflow
Solution 7 - JavascriptJorge Santos NeillView Answer on Stackoverflow
Solution 8 - Javascriptjonathan klevinView Answer on Stackoverflow
Solution 9 - JavascriptJobelleView Answer on Stackoverflow
Solution 10 - JavascriptUmar NiaziView Answer on Stackoverflow
Solution 11 - JavascriptOmzigView Answer on Stackoverflow