Page 1 of 1

cancelBubble

Posted: Tue Apr 24, 2007 10:28 pm
by sarris
Hi there. I have a link on a <div> element which has a function assigned to its onclick event.
I am trying to make it so when i click the link only the link executes and not the onclick event of the <div>
I am trying various things with cancelBubble but i guess its no worth it as the function that applies to the link doesnt execute at all.
my code is that

Code: Select all

<div onclick = "javascript: myfunction1()">
<a href = "javascript: my function2()">Click here for function</a>
<div/>

Code: Select all

function myfunction2(){
alert("HALLO");
}

function myfunction1(){
alert("WOW");
}
Any ideas?

Posted: Wed Apr 25, 2007 8:49 am
by sarris
actually if i put

Code: Select all

event.cancelBubble=true;
at function 2 it works ok with IE.
I tried

Code: Select all

event.stopPropagation();
according to the W3 model but doesnt work with mozzila still.
Any ideas plz?

Posted: Thu Apr 26, 2007 1:21 pm
by Kieran Huggins
from PPK: http://www.quirksmode.org/js/events_order.html

Code: Select all

function doSomething(e)
{
	if (!e) var e = window.event;
	e.cancelBubble = true;
	if (e.stopPropagation) e.stopPropagation();
}

Posted: Thu Apr 26, 2007 1:53 pm
by sarris
aaah very thanks! i had found the solution a few minutes ago in the exact same link!!
thanks anyway.people may find it useful.