68

just a quick question. I'm having a problem with divs with onclick javascript within each other. When I click on the inner div it should only fire it's onclick javascript, but the outer div's javascript is also being fired. How can the user click on the inner div without firing the outer div's javascript?

<html>
<body>
<div onclick="alert('outer');" style="width:300px;height:300px;background-color:green;padding:5px;">outer div
    <div onclick="alert('inner');"  style="width:200px;height:200px;background-color:white;" />inner div</div>
</div>
</div>
</body>
</html>
Daniel Brink
  • 2,434
  • 4
  • 24
  • 26

13 Answers13

128

Basically there are two event models in javascript. Event capturing and Event bubbling. In event bubbling, if you click on inside div, the inside div click event fired first and then the outer div click fired. while in event capturing, first the outer div event fired and than the inner div event fired. To stop event propagation, use this code in your click method.

   if (!e) var e = window.event;
    e.cancelBubble = true;
    if (e.stopPropagation) e.stopPropagation();
Adeel
  • 19,075
  • 4
  • 46
  • 60
28

Check out the info on event propagation here

In particular you'll want some code like this in your event handlers to stop events from propagating:

function myClickHandler(e)
{
    // Here you'll do whatever you want to happen when they click

    // now this part stops the click from propagating
    if (!e) var e = window.event;
    e.cancelBubble = true;
    if (e.stopPropagation) e.stopPropagation();
}
Tim Goodman
  • 23,308
  • 7
  • 64
  • 83
8

This is a case of event bubbling.

You can use

e.cancelBubble = true; //IE

and

e.stopPropagation(); //FF
rahul
  • 184,426
  • 49
  • 232
  • 263
6

Just add this code :

window.event.stopPropagation();
user3438083
  • 69
  • 1
  • 3
6

return false; from the inner div's onclick function:

<div onclick="alert('inner'); return false;" ...

What you're dealing with is called event propagation.

Tatu Ulmanen
  • 123,288
  • 34
  • 187
  • 185
  • The method you suggest doesn't actually work. (At least not in any of the browsers I just double checked it on-- Firefox 3.6, IE8.) Note that your link actually says to use a different method, which I gave in my answer. – Tim Goodman Mar 05 '10 at 07:37
5

One more way for webkit based browsers:

<div onclick="alert('inner'); event.stopPropagation;" ...
Juliano Moraes
  • 169
  • 2
  • 3
2

This worked for me in Jquery:

$('#outer_element').click(function(event) {
    if(event.target !== event.currentTarget) return;
    alert("Outer element is clicked")                       
});

This way, if the current target is not the same as the outer div, it will do nothing. You can implement normal functions on the child elements.

Ramtin
  • 3,058
  • 1
  • 22
  • 24
1

This was very helpful, but it didn't work for me.

What i did is described here.

So I put a condition to the outer onclick event:

if( !event.isPropagationStopped() ) {
    window.location.href = url;
}
Community
  • 1
  • 1
shooby
  • 95
  • 2
  • 8
1

Here is some more reference to help you in understanding javascript event bubbling.

Ben
  • 784
  • 3
  • 12
  • 32
1

You can use

    $("divOrClassThatYouDontWantToPropagate").click(function( event ) {
      event.stopPropagation();
    });
AngelQuesada
  • 387
  • 4
  • 19
1

you have two 'div' and three '/div'.

freddie
  • 11
  • 1
1
 //Using window.event.stopPropagation(), the parent div wont be clicked when the child div is clicked
 //HTML    
 <div onclick="parent_func()">
  <div onclick="child_func()"></div>
 </div>

//JS
function parent_func(){
  window.event.stopPropagation();
  console.log('Parent')
}
function child_func(){
  window.event.stopPropagation();
  console.log('Child')
}
Paul Jere
  • 11
  • 4
0

You have to stop propragation for example for angular framework you have to do that

import {Directive, HostListener} from "@angular/core";
    
@Directive({
    selector: "[click-stop-propagation]"
})
export class ClickStopPropagation
{
    @HostListener("click", ["$event"])
    public onClick(event: any): void
    {
        event.stopPropagation();
    }
}

Then just add it to the element you want it on:

<div click-stop-propagation>Stop Propagation</div>