views:

91

answers:

3

This is what I have basically:

<a href="http://somelink.com"&gt;
    <span>stuff</span>
    <span onclick="AwesomeFunction();">more stuff</span>
    <span>the last stuff</span>
</a>

Now, the problem is I want to keep the parent as a link but if the user clicks the span with the onclick event I don't want the browser to follow the link.

I've tried

event.stopPropagation();

but that only seems to stop the links onclick event from firing, or I'm doing something wrong.

I'm currently sort of in crunch mode and I don't want to spend too much time rewriting the code that generates this HTML, but it still can't be a hack since it's implemented in a pretty vital function of the site. Any help appreciated.

+3  A: 

sure, just return false in the onclick

something like

<a href="somwhere" onclick="return false;">nothing happens</a>
Victor
Well... That's not quite right. He still wants the follow the link when he clicks the other spans. This code will stop the browser from following the link an any click on it. Not just the span with the onClick event.
Sani Huttunen
that is just an example of an A that does not follow its href onclick...
Victor
+4  A: 
<a href="http://somelink.com"&gt;
    <span>stuff</span>
    <span onclick="AwsomeFunction(); return false;">more stuff</span>
    <span>the last stuff</span>
</a>
Sani Huttunen
+2  A: 

Make the javascript method return false!

You can also you event.preventDefault() if your using jQuery

Carl Bergquist
Doh! Thanks. Can't believe I missed that...
Oscar Kilhed