I have a textbox
<input type="text" id="textInput">
with the following JavaScript:
document.addEventListener('keydown', function(event) {
alert('Key Pressed');
});
Would it be possible to trigger the alert only when the input is not selected?
I have a textbox
<input type="text" id="textInput">
with the following JavaScript:
document.addEventListener('keydown', function(event) {
alert('Key Pressed');
});
Would it be possible to trigger the alert only when the input is not selected?
You can check for event target and then trigger the command.
document.addEventListener('keydown', function(event) {
if (!event === document.querySelector("#textInput")) {
alert('Key Pressed');
}
});
Hope This Helps !
Yes it is possible, check document.activeElement
to see which element has focus:
document.addEventListener('keydown', function(event) {
if (document.activeElement && document.activeElement.id !== 'textInput'){
alert('Key Pressed');
}
});