This is simplified problem I am facing.
I have a button. When this button is clicked it will dispatch custom event customClick
. So I want other DOM
elements, like input, to listen for this custom events.
But input does not listen. Only button which dispatch event would listen to customClick
.
What I am doing wrong? How can I make input element listen to customClick
event?
var button = document.getElementById('customClick');
button.addEventListener('click', function(e){
var clickedButton = e.currentTarget;
var newEvent = new CustomEvent("customClick");
//console.log(newEvent);
clickedButton.dispatchEvent(newEvent);
})
button.addEventListener('customClick', function(e){
alert('button listening customClick');
})
var input = document.getElementById('input');
input.addEventListener('customClick', function(e){
alert('input listening customClick');
})
<button id="customClick">custom click</button>
<input id="input" type="text" />