283

I have a DIV with a classed foobar, and a few DIVs inside that DIV that are unclassed, but I suppose they are inheriting the foobar class:

$('.foobar').on('click', function() { /*...do stuff...*/ });

I want that to fire off only when clicking somewhere in the DIV but not on its children DIVs.

12 Answers 12

520

If the e.target is the same element as this, you've not clicked on a descendant.

$('.foobar').on('click', function(e) {
  if (e.target !== this)
    return;
  
  alert( 'clicked the foobar' );
});
.foobar {
  padding: 20px; background: yellow;
}
span {
  background: blue; color: white; padding: 8px;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>
<div class='foobar'> .foobar (alert) 
  <span>child (no alert)</span>
</div>

13
  • 3
    Hi @vicky. Just FYI, JavaScript has two different kinds of value equality comparison. The === or !== uses the "Strict Equality Comparison Algorithm", while the == and != uses the "Abstract Equality Comparison Algorithm", which is type coercive. The general wisdom is to always use strict comparison unless there's specific need for the coercive type (since its rules are a little complex). In this case, because objects are being compared, it really doesn't make a difference, but you'll find that most people will still stick with the strict comparison.
    – user1106925
    Jul 9, 2015 at 18:26
  • 1
    @vicky: That's alright. It's still correct. Just letting you JS has the different operators. ;-)
    – user1106925
    Jul 11, 2015 at 0:23
  • 1
    @GauravAggarwal: The purpose of having the if statement first is to make it so that the code below it does not run when clicking on children of the .foobar element. If we put the alert on top, it will always run. So basically, you should see the alert when clicking on the outer yellow area, but not on the blue child.
    – user1106925
    Dec 3, 2015 at 15:22
  • 3
    This is a good answer because it can also work with Vanilla JS addEventListener and is not jQuery specific. Aug 12, 2016 at 12:15
  • 1
    how to do this on vanilla js? Dec 19, 2020 at 2:10
124

I did not get the accepted answer to work, but this seems to do the trick, at least in vanilla JS.

if(e.target !== e.currentTarget) return;
6
  • 8
    e.currentTarget is more accurate than referencing this because the object that this is pointing to can change depending on the scope and context from where it is called
    – eballeste
    Jul 15, 2019 at 19:38
  • 5
    event.currentTarget looks to be the cleanest method here. Aug 17, 2020 at 1:27
  • 4
    note that you can also do if (e.target === e.currentTarget) { ...your code } why this works: e.target is the element that triggered the event (e.g., the user clicked on) e.currentTarget is the element that the event listener is attached to. so when you compare these two the element which the event is attached is same as the one was clicked. Feb 5, 2021 at 20:25
  • Thanks the solution works perfectly for my problem. I observed is that when you log "e" and open the logged object currentTarget is null but when you log "e.currentTarget" it logs the element correctly. I am confused why? Jul 9, 2021 at 9:31
  • 1
    best answer for sure
    – Cybernetic
    Mar 1, 2022 at 3:18
111

There's another way that works if you don't mind only targeting newer browsers. Just add the CSS

pointer-events: none;

to any children of the div you want to capture the click. Here's the support tables

http://caniuse.com/#feat=pointer-events

5
  • 33
    Know that I should avoid writing 'thanks' comments, but I can kiss you foot for this :-) Mar 29, 2017 at 9:24
  • @SimonaAdriani huh?
    – n3wb
    Jul 23, 2018 at 23:21
  • Thanks, I did the same way, but I'm struggling to write unit test case around it. How can I make sure click programatic click event trigger should be blocked? Dec 16, 2018 at 6:44
  • This is not correct. You may still want to allow events on the child element, just not the parent element. For example, the parent might have a mouseover event, which you don't want on the child element that is an input, but obviously you want to use the input.
    – Cybernetic
    Mar 1, 2022 at 3:15
  • 1
    @Cybernetic - In that case you can just handle the click (or focus) event on the parent and then focus the input with input.focus(), but in any case there's multiple solutions to this problem and which one someone uses will be based on their use case anyway. Mar 1, 2022 at 17:21
37

You can use bubbling in your favor:

$('.foobar').on('click', function(e) {
    // do your thing.
}).on('click', 'div', function(e) {
    // clicked on descendant div
    e.stopPropagation();
});
2
  • This looks like a nice solution for my specific scenario, as I only want to exclude clicks on descendant <a> elements. There is just one problem: When I middle click on them the event still triggers (tested in Google Chrome). Is there a variant of this that also prevents this?
    – cgogolin
    Oct 19, 2015 at 9:23
  • @cgogolin, take a look at this: stackoverflow.com/questions/12197122/…
    – Jessica
    Jun 10, 2019 at 11:47
23
//bind `click` event handler to the `.foobar` element(s) to do work,
//then find the children of all the `.foobar` element(s)
//and bind a `click` event handler to them that stops the propagation of the event
$('.foobar').on('click', function () { ... }).children().on('click', function (event) {
    event.stopPropagation();
    //you can also use `return false;` which is the same as `event.preventDefault()` and `event.stopPropagation()` all in one (in a jQuery event handler)
});

This will stop the propagation (bubbling) of the click event on any of the children element(s) of the .foobar element(s) so the event won't reach the .foobar element(s) to fire their event handler(s).

Here is a demo: http://jsfiddle.net/bQQJP/

6

I had the same problem and came up with this solution (based on the other answers)

 $( ".newsletter_background" ).click(function(e) {
    if (e.target == this) {
        $(".newsletter_background").hide();
    } 
});

Basically it says if the target is the div then run the code otherwise do nothing (don't hide it)

1
  • Can you use an arrow function here instead of (function(e) {})?
    – Zaffer
    Sep 12, 2021 at 22:24
5
$(".advanced ul li").live('click',function(e){
    if(e.target != this) return;
    //code
    // this code will execute only when you click to li and not to a child
})
3

You can use event.currentTarget. It will do click event only elemnt who got event.

target = e => {
    console.log(e.currentTarget);
  };
<ul onClick={target} className="folder">
      <li>
        <p>
          <i className="fas fa-folder" />
        </p>
      </li>
    </ul>

1
  • currentTarget doesn't decide if the event is executed. It just holds the reference to the element.
    – shaedrich
    Sep 5, 2022 at 8:59
2

If you can't use pointer-events: none; and are targeting modern browsers you can use composedPath to detect a direct click on the object like so:

element.addEventListener("click", function (ev) {
    if (ev.composedPath()[0] === this) {
        // your code here ...
    }
})

You can read more about composedPath here: https://developer.mozilla.org/en-US/docs/Web/API/Event/composedPath

1

My case is similar but this is occasion when you have few foobar-s, and you want to close only one - per one click:

Find parent case

$(".foobar-close-button-class").on("click", function () {
    $(this).parents('.foobar').fadeOut( 100 );
    // 'this' - means that you finding some parent class from '.foobar-close-button-class'
    // '.parents' -means that you finding parent class with name '.foobar'
});

Find child case

$(".foobar-close-button-class").on("click", function () {
    $(this).child('.foobar-close-button-child-class').fadeOut( 100 );
    // 'this' - means that you finding some child class from '.foobar-close-button-class'
    // '.child' -means that you finding child class with name '.foobar-close-button-child-class'
});
1

When you define an event, the event has a property this. This property represents the DOMElement to which the event was assigned.To check the element in which the event was fired, use e.target.

Since events are inherited in the children of an element, check if the target

function doSomething(event) {
  if (this == event.target){
    // do something
  }
}
0

// if its li get value 
document.getElementById('li').addEventListener("click", function(e) {
                if (e.target == this) {
                    UodateNote(e.target.id);
                }
                })
                
                
                function UodateNote(e) {

    let nt_id = document.createElement("div");
    // append container to duc.
    document.body.appendChild(nt_id);
    nt_id.id = "hi";
    // get conatiner value . 
    nt_id.innerHTML = e;
    // body...
    console.log(e);

}
li{
 cursor: pointer;
    font-weight: bold;
  font-size: 20px;
    position: relative;
    width: 380px;
    height: 80px;
    background-color: silver;
    justify-content: center;
    align-items: center;
    text-align: center;
    margin-top: 0.5cm;
    border: 2px solid purple;
    border-radius: 12%;}
    
    p{
     cursor: text;
  font-size: 16px;
   font-weight: normal;
    display: block;
    max-width: 370px;
    max-height: 40px;
    overflow-x: hidden;}
<li id="li"><p>hi</p></li>

Your Answer

Reminder: Answers generated by Artificial Intelligence tools are not allowed on Stack Overflow. Learn more

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.