2

I want the event only fired when I click the div itself, the problem is it will also fired when I click the button, which is not what I want. What should I do?

document.querySelector('.container').addEventListener('click', e => {
    console.log('click div');
});
<div class="container" >
    <button>Click me</button>
</div>
Brian Tompsett - 汤莱恩
  • 5,753
  • 72
  • 57
  • 129
jr6tp
  • 485
  • 1
  • 5
  • 15

5 Answers5

3

You can check if the event target matches the current target, and only then run the callback function:

eventOnCurrentElement(".container", "click", e => {
   console.log("clicked div");
});

function eventOnCurrentElement(selector, event, callback) {
  document.querySelector(selector).addEventListener(event, e => {
    if (e.currentTarget === e.target) {
      callback(e)
    }
  });
}
<p>Click beside the button:</p>
<div class="container">
  <button>Click me</button>
</div>

e.currentTarget always refers to the element to which the event handler has been attached, as opposed to e.target which identifies the element on which the event occurred.

Luca Kiebel
  • 9,790
  • 7
  • 29
  • 44
0

Add this into your code:

document.querySelector('button').addEventListener('click', e => {

    e.stopPropagation();
});

Your issue was due to eventPropagation, the button click event bubbles up into the div click event.

Check eventPropagation here.

Janice Zhong
  • 836
  • 7
  • 16
0

You can check if the target is actually the container or a descendant like so:

var container = document.querySelector('.container');
container.addEventListener('click', e => {
  if (e.target !== container)
    return;
  console.log('click div');
});

https://codepen.io/anon/pen/dqaNdR

Ryan Marin
  • 94
  • 1
  • 5
-1

Check if the target is equal to the currentTarget

document.querySelector('.container').addEventListener('click', e => {
    if(e.target !== e.currentTarget)
        return false;
    console.log('div');
    //Your code
});
<div class="container" >
    <button>Click me</button>
</div>

If you are using jQuery;

$(document).on('click', '.container', function(event){
    if(event.target !== this)
        return false;
    console.log('div');
    //Your code
})
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div class="container" >
    <button>Click me</button>
</div>
ssten
  • 1,848
  • 1
  • 16
  • 28
-1

You have to check event target and currentTarget as follow code:

<div class="container">
  <button>Click me</button>
</div>
container.addEventListener('click', (e) => {
    if (event.target === e.currentTarget) {
      // any code
      console.log('click div');
    }
});
  • 1
    This is a lump of code which completely lacks anything resembling an explanation for what was changed or why it should solve the problem. It reads more like a game of spot-the-difference than an answer. (It looks like you are trying to say the same as [this answer](https://stackoverflow.com/a/52422769/19068) which was posted a good five minutes before yours). – Quentin Sep 20 '18 at 10:25
  • 1
    I wrote this answer while another answer was published... I didn't know that question was already answered. – Maksym Tymchyk Sep 21 '18 at 13:39