input submit on enter key code example
Example 1: javascript submit form when enter key pressed
document.getElementById("myInputID").addEventListener("keyup", function(event) {
if (event.keyCode === 13) {
document.getElementById("myFormID").submit();
return false;
}
});
Example 2: html button press enter off
Explicit Prevention:
<form>
<label for="name">Name:</label>
<input type="text" name="name" onkeypress="ExplicitPrevention(event)">
<input type="submit" value="Submit">
</form>
<script>
const ExplicitPrevention = function (event) {
var keyPressed = event.keyCode || event.which;
if (keyPressed === 13) {
alert("Test: You pressed the Enter key!!");
event.preventDefault();
}
}
</script>