<div onclick="AutogeneratedMethod()">
<span>SPAN 1 : I require auto method </span>
<span onclick="DesiredMethod()">SPAN 2 : I do not require auto method</span>
</div>
In the above code sample, how do I prevent calling the AutogeneratedMethod() when the user clicks on the SPAN 2? I do not have any control on the declaration of the div and its respective onclick behavior. That line is auto-generated from the framework I am using.
I'm looking for something that would kill Javascript execution at the end of the DesiredMethod(). Something like this:
function DesiredMethod() {
//Do necessary stuff
exit; //Abort any further Javascript
}
-
execute invalid code, it will totally prevent any further JavaScript :)ajax333221– ajax3332212012年06月05日 17:48:54 +00:00Commented Jun 5, 2012 at 17:48
-
Is there any jQuery somewhere else in your code?Blazemonger– Blazemonger2012年06月05日 17:54:25 +00:00Commented Jun 5, 2012 at 17:54
2 Answers 2
You want to stop event propagation. IE has cancelBubble.
Comments
You could also just move things around a bit like this:
<div>
<span onclick="AutogeneratedMethod()">SPAN 1 : I require auto method </span>
<span onclick="DesiredMethod()">SPAN 2 : I do not require auto method</span>
</div>
Then you wouldn't need to worry about event propagation and bubbling.
You may also want to consider attaching your JavaScript events via a different mechanism either with a framework(jQuery, etc...) or with pure JavaScript. Doing it via HTML attributes is frowned upon for many reasons. Check out this link for more details.