cancelBubble

JavaScript and client side scripting.

Moderator: General Moderators

Post Reply
sarris
Forum Contributor
Posts: 137
Joined: Mon Dec 04, 2006 2:44 pm

cancelBubble

Post 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?
sarris
Forum Contributor
Posts: 137
Joined: Mon Dec 04, 2006 2:44 pm

Post 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?
User avatar
Kieran Huggins
DevNet Master
Posts: 3635
Joined: Wed Dec 06, 2006 4:14 pm
Location: Toronto, Canada
Contact:

Post 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();
}
sarris
Forum Contributor
Posts: 137
Joined: Mon Dec 04, 2006 2:44 pm

Post 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.
Post Reply