js press enter code example
Example 1: javascript key pressed enter
$('.container').on('keydown', 'input', function(e) {
if (e.keyCode === 13) {
e.preventDefault();
e.stopImmediatePropagation();
}
});
Example 2: javascript detect enter press on input
var el = document.getElementById("myInputID");
el.addEventListener("keyup", function(event) {
if (event.key === "Enter") {
}
});
Example 3: 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>
Example 4: input enter button js
var input = document.getElementById("myInput");
input.addEventListener("keyup", (event) => {
if (event.key === "Enter") {
event.preventDefault();
document.getElementById("myBtn").click();
}
})