addEventListener on enter code example
Example 1: enter event in jquery
$('#textbox').keypress(function(event){
var keycode = (event.keyCode ? event.keyCode : event.which);
if(keycode == '13'){
alert('You pressed a "enter" key in textbox');
}
});
Example 2: javascript enter event listener
document.querySelector('#txtSearch').addEventListener('keypress', function (e) {
if (e.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>