12

I have two divs, a big one and a smaller over the other, each div has its own OnClick method. The problem I have is when I clicked the smaller div, the big div's OnClick method is called too.

Who can I avoid that?

sth
  • 222,467
  • 53
  • 283
  • 367
Santiago
  • 2,190
  • 10
  • 30
  • 59

6 Answers6

8

Your problem is that the click event will propagate up the element tree. Therefore, each element that contains an element that was clicked will also fire a click event.

The simplest solution is to add return false your handler.

If you're using jQuery, you can call e.stopPropagation(); otherwise, you'll need to call e.stopPropagation() if it exists, and set event.cancelBubble = true.

For more information, see here.

SLaks
  • 868,454
  • 176
  • 1,908
  • 1,964
  • Thank you for this it works, but there's a minor typo. You wrote `e.StopPropagation();` but it should be `e.stopPropagation();` – Mario Zigliotto Feb 17 '13 at 21:17
7

The best way to detect which element was clicked is to analyze target of event ( click event ). I have prepared small example for this case. You can see it in code below.

function amIclicked(e, element)
{
    e = e || event;
    var target = e.target || e.srcElement;
    if(target.id==element.id)
        return true;
    else
        return false;
}
function oneClick(event, element)
{
    if(amIclicked(event, element))
    {
        alert('One is clicked');
    }
}
function twoClick(event, element)
{
    if(amIclicked(event, element))
    {
        alert('Two is clicked');
    }
}

This javascript method can be called before you execute your script

Example

<style>
#one
{
    width: 200px;
    height: 300px;
    background-color: red;
}
#two
{
    width: 50px;
    height: 70px;
    background-color: yellow;
    margin-left: 10; 
    margin-top: 20;
}

</style>



<div id="one" onclick="oneClick(event, this);">
    one
    <div id="two" onclick="twoClick(event, this);">
        two
    </div>
</div>

I hope this helps.

Senad Meškin
  • 13,597
  • 4
  • 37
  • 55
  • It finally works mixing answers: event.cancelBubble = true; if (event.stopPropagation) event.stopPropagation(); window.event dont work on Firefox, dont know why – Santiago Jan 06 '10 at 19:23
  • `window.event` is a non-standard IE property. – SLaks Jan 06 '10 at 19:26
4

What you're dealing with is event bubbling. Take a look at this article: http://www.quirksmode.org/js/events_order.html.

Basically, to stop the event from passing to the parent element, you can use something like this:

document.getElementById('foo').onClick = function(e) {

    // Do your stuff

    // A cross browser compatible way to stop propagation of the event:
    if (!e) var e = window.event;
    e.cancelBubble = true;
    if (e.stopPropagation) e.stopPropagation();
}
Tatu Ulmanen
  • 123,288
  • 34
  • 187
  • 185
2

You're running into a common case of event propagation. Check out quirksmode.org to get the full details on what exactly is happening. Basically, what you need to do in the smaller div's click handler is this:

if (!e) var e = window.event;
e.cancelBubble = true;
if (e.stopPropagation) e.stopPropagation();
Shawn Steward
  • 6,773
  • 3
  • 24
  • 45
0

If you decide to use a javascript library such as www.jquery.com you can easily accomplish what you are trying to do by using the propagation prevention options.

code_burgar
  • 12,025
  • 4
  • 35
  • 53
0

This worked for me in Jquery:

$('#outer_div').click(function(event) {
    if(event.target !== event.currentTarget) return;
    alert("outer div 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 for the child element.

Ramtin
  • 3,058
  • 1
  • 22
  • 24