If I add an event Listener on click I get the actual element clicked on, whether it's the element I added the listener to or a child element.
But if I do the same for input
, keyup
, keydown
, or keypress
on a content-editable element, I only get the element the event listener is added on, not child elements, even if the input happened in the child element.
How can I also get the child element when the key is pressed on it?
For instance: Click the word "Child" in the content-editable div below and type, and you'll see that e.target
is the div
, not the span
:
document.getElementsByTagName("div")[0].addEventListener("click",function(e){
document.getElementsByTagName("b")[0].innerHTML = e.target;
});
document.getElementsByTagName("div")[1].addEventListener("input",function(e){
document.getElementsByTagName("b")[0].innerHTML = e.target;
});
Click:
<div>Parent
<span>Child</span>
</div>
<br>
Input:
<div contenteditable>Parent
<span>Child</span>
</div>
<br>
<div>Target:<b></b></div>