This article will show you how to differentiate click on a div and a click on one of it’s children.


$('.outer').on('click', function(event) {
if ( !== this)
// Your code

The issue

You want to respond to click events on the red div, but when you click the green div, the event is triggered as well.


$('.red').click(function() {console.log('click')});

will be executed with a click on the red and green divs.

Solution with jQuery

jQuery provides an property that represents the element that was effectively clicked. By comparing it to the element with the click event attached, it is possible to know is the event is being triggered due to bubbling.

$('.red').on('click', function(event) {
// compare the element clicked ( with the
// element that has the click attached (this)
if ( !== this)
console.log('red div was clicked')

Solution in vanilla JavaScript

Native JavaScript provides the same property, here is how to use it:

var redDiv = document.getElementById('red');
redDiv.onclick = function (e) {
var ev = e || window.event;
if( !== this)
console.log('red div was clicked');

window.event is the IE way of retrieving the event information.