javascript on key press code example
Example 1: jquery on enter key pressed
$(document).on('keypress',function(e) {
if(e.which == 13) {
alert('User pressed Enter!');
}
});
Example 2: js when key is pressed
document.addEventListener("keypress", function(event) {
});
Example 3: javascript key pressed enter
$('.container').on('keydown', 'input', function(e) {
if (e.keyCode === 13) {
e.preventDefault();
e.stopImmediatePropagation();
}
});
Example 4: html onkeypress
<input type="text" onkeypress="myFunction()">
Example 5: adding event listener keypress event in javascript
let msg = document.getDocumentById('#message');
msg.addEventListener("keydown", (event) => {
});
msg.addEventListener("keypress", (event) => {
});
msg.addEventListener("keyup", (event) => {
});