Enter Key Event in JavaScript
This tutorial demonstrates the different ways of using the JavaScript enter key event
to stop the form submission every time the Enter key is pressed.
JavaScript Enter Key Event to Prevent Form Submission
We get the element with id value form
and save that into the element
variable in the following example.
After that, the addEventListener()
method is used to attach an event handler to the element
. Whenever the Enter key is pressed, the form submission is prevented by using event.preventDefault()
.
You can read more about this function at this page. The event.key
tells the key name, and event.which
shows the keycode.
HTML Code:
<!DOCTYPE html>
<html>
<head>
<title>Enter Key Event</title>
</head>
<body>
<form action="#" method = "POST" id="form">
<label>First name:</label>
<input type="text" id="first-name" name="first-name">
<br />
<label>Last name:</label>
<input type="text" id="last-name" name="last-name">
<br />
<input id="submitBtn" type="submit" value="Submit" />
</form>
</body>
</html>
JavaScript Code:
var element = document.getElementById('form');
element.addEventListener('keypress', function(event) {
if (event.key === 'Enter') {
alert(event.key + ' ' + event.which);
event.preventDefault();
}
});
In the previous example, we are detecting the Enter within the <form>
element. But the code given below prevents the form submission if the Enter key is pressed.
document.addEventListener('keypress', function(e) {
if (e.keyCode === 13 || e.which === 13) {
e.preventDefault();
return false;
}
});
/*we can write the same logic in jQuery as follows. You
can copy the jQuery code and uncomment to practice. Make
sure to include the jQuery library.*/
/*
$(document).ready(function() {
$(window).keydown(function(event){
if(event.keyCode == 13) {
event.preventDefault();
return false;
}
});
});
*/
The click
event does not let the Enter key to submit the form.
Every time the element whose id’s value is submitBtn
is clicked, the form would be submitted. See the following example.
document.getElementById('submitBtn').addEventListener('click', function() {
form.submit();
});
The approach below is not advised because HTML and JavaScript must be in separate files to organize the code.
Still, you can also use the onkeypress
property within the <input>
element with type=submit
, which prevents the form submission if the Enter key is pressed.
We can also use this property for each input field. Check the following example.
HTML Code:
<!DOCTYPE html>
<html>
<head>
<title>Enter Key Event</title>
</head>
<body>
<form action="#" method = "POST" id="form">
<label>First name:</label>
<input type="text" id="first-name" name="first-name" onkeypress="return
(event.keyCode!=13);">
<br />
<label>Last name:</label>
<input type="text" id="last-name" name="last-name" onkeypress="return
(event.keyCode!=13);">
<br />
<input id="submitBtn" type="submit" value="Submit" onkeypress="return
(event.keyCode!=13);" />
</form>
</body>
</html>
We can also make a separate function in the JavaScript file and call it if the particular button is clicked. The form will only submit if that button is clicked; otherwise, not.
See the following code.
HTML Code:
<!DOCTYPE html>
<html>
<head>
<title>Enter Key Event</title>
</head>
<body>
<form action="#" method = "POST" id="form">
<label>First name:</label>
<input type="text" id="first-name" name="first-name">
<br />
<label>Last name:</label>
<input type="text" id="last-name" name="last-name">
<br />
<input id="submitBtn" type="submit" value="Submit" onclick="myfunction()" />
</form>
</body>
</html>
JavaScript Code:
document.addEventListener('keypress', function(e) {
if (e.keyCode === 13 || e.which === 13) {
e.preventDefault();
return false;
}
});
function myfunction() {
document.getElementById('form').submit();
}