Попробуйте приведенный ниже код. В focus
удалены все прослушиватели событий, а в onblur
добавлены все прослушиватели событий.
let input = document.getElementById('input');
//Attach all the events
input.addEventListener('focus', removeAllEventListner);
input.addEventListener('keypress', myFunction);
input.addEventListener('blur', addEvents);
//On focus remove all the events
function removeAllEventListner() {
input.removeEventListener('keypress', myFunction);
console.log('Event removed');
}
//Onblur add all event
function addEvents() {
input.addEventListener( 'onkeypress', myFunction);
console.log('Event attached');
}
function myFunction() {
console.log('Keypress event');
}
<input type="text" id="input" value="" >
Надеюсь, что это решит проблему.