views:

895

answers:

6

Ok, simple question:

<div onclick="javascript:manualToggle(this)">
    <span>Allowed to click</span>
    <span>Not allowed to click</span>
    <span>Allowed to click</span>
</div>

Without replicating the manualToggle on to the 2 spans that are allowed to click, how can I prevent the "Not allowed to click" span from firing it's parent div onclick event when it is clicked on?

A: 

Possible solution: give the span's id's and check whether the clicked id is allowed to be clicked in your function

bad idea: you don't know which span is clicked since you call the function from your div...

Natrium
A: 

with mootools you can use the method stopPropagation:

$('myChild').addEvent('click', function(event){
    event.stopPropagation(); // this will prevent the event to bubble up, and fire the parent's click event.
});

see http://mootools.net/docs/core/Native/Event#Event%3AstopPropagation

also see this very similar question: http://stackoverflow.com/questions/985389/how-can-i-stop-an-onclick-event-from-firing-for-parent-element-when-child-is-clic

knittl
yay, downvote a perfectly valid (and working) answer!
knittl
downvote prolly for the js-library you suggest, just like phoenix who suggest jQuery
Natrium
using libraries is the easiest (and most browser-compatible) way
knittl
I used the accepted answer to swap a few bits round and use jQuery to achieve what I needed. Thank you for your answer though.(I haven't down voted anything in this question...)
Paul
+2  A: 

You need an event handler (it's very easy to do this in something like jQuery) that catches clicks for the spans within the div and only fires the function if, for example, the span has/hasn't a particular class.

meanstreakuk
+1  A: 

Give the span an id an attach onclick event to it and use

A jQuery sample

$("#spn2").click(function(event){
  event.stopPropagation();  
});

event.stopPropagation(): Stops the bubbling of an event to parent elements, preventing any parent handlers from being notified of the event.

rahul
Is the down vote for a jQuery answer
rahul
I guess so
Natrium
I went for something similar, but not with stopPropagation.(I haven't down voted anything in this question...)
Paul
+1  A: 

It would make sense to use a library but without you can try this (edited with an entire page to test):

<html><head></head>
<body>
<script type="text/javascript"><!--
function manualToggle(val)
{
    alert(val.id);
}

--></script>

<div id="test" onclick="manualToggle(this);">
    <span>Allowed to click</span>
    <span onclick="event.cancelBubble=true;if (event.stopPropagation) event.stopPropagation();">Not allowed to click</span>
    <span>Allowed to click</span>
</div>
</body>
</html>
Daff
`event`? `e`? there seems to be missing something
knittl
Thx, e doesn't work I only tested it in a cancelBubble Browser, I changed it to event (which is window.event) and put some testing code in there. It works at least in FF 3.5 and IE7
Daff
A: 

you fnord is no help! its the second time i end up here. you fools tell everyone use jquery BULLSHIT!

function event(e)

{

if(!e) e = window.event if(e.srcElement) e.target = e.srcElement; return e; }

function clear(e)

{

e = event(e); e.fromchild = e.target.parentNode;

}

function gotoCategory(e)

{

e = event(e); trace(e,e.target, e.fromchild);

if(e.fromchild) // FYI GFY

else //notfromchild

}

function trace()

{

try{console.log(trace.arguments)}catch(e){return;}

}

wrapper.onclick = gotoCategory;

img.onclick = clear;

title.onclick = clear;

Peter