D
D
Daniil Popov2018-05-02 09:05:58
JavaScript
Daniil Popov, 2018-05-02 09:05:58

Why is chrome ignoring the event handler?

In chrome, the parent node handler intercepts the click and the child element handler does not fire. FF executes both handlers. If you leave two identical events (onmousedown only or onclick only), everything works correctly.
HTML

<section id = "parent">
  <div id = "child">
    Child
  </div>
</section>

JS
var child = document.querySelector("#child");
var parent = document.querySelector("#parent");

child.onclick = function(){
  alert("Привет, я - Child");
}

parent.onmousedown = function(){
  alert("Привет, я - Parent");
}

Here is an example on jsfiddle

Answer the question

In order to leave comments, you need to log in

2 answer(s)
D
Dmitry Luzanov, 2018-05-02
@dmitry_luzanov

child.addEventListener('click', function(){
  alert("Привет, я - Child");
})

child.addEventListener('mousedown', function(){
  alert("Привет, я - Parent");
})

Read .

K
Kim_Soal, 2018-05-02
@Kim_Soal

Indeed, it didn’t work like that
. Probably, this is some kind of feature of event bubbling in chrome.
You can just check the targets if you don’t bother

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question