Two differents OnClick on two divs, one over the other?

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.

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 one two I hope this helps.

Helps a lot, I'm going to try, thanks! – Santiago Jan 6 '10 at 19:08 It finally works mixing answers: event. CancelBubble = true; if (event.

StopPropagation) event.stopPropagation(); window. Event don't work on Firefox, don't know why – Santiago Jan 6 '10 at 19:23 window. Event is a non-standard IE property.

– SLaks Jan 6 '10 at 19:26.

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.

Thanks, that's what I thought but It simple doesn't work. – Santiago Jan 6 '10 at 18:05.

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().

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

You don't need to use jQuery or anything else, it's easily done with plain javascript. – Shawn Steward Jan 6 '10 at 18:00.

What you're dealing with is event bubbling. Take a look at this article: 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(); }.

1 The first line of that function could be shortened to e = e || window. Event; – gnarf Jan 6 '10 at 18:01 It works on Chrome but Firefox said "window. Event is undefined" – Santiago Jan 6 '10 at 19:07.

I cant really gove you an answer,but what I can give you is a way to a solution, that is you have to find the anglde that you relate to or peaks your interest. A good paper is one that people get drawn into because it reaches them ln some way.As for me WW11 to me, I think of the holocaust and the effect it had on the survivors, their families and those who stood by and did nothing until it was too late.

Related Questions