How to prevent default in an onclick method? I have a method in which I am also passing a custom value
<a href="#" onclick="callmymethod(24)">Call</a>
function callmymethod(myVal){
//doing custom things with myVal
//here I want to prevent default
}
Let your callback return
falseand pass that on to theonclickhandler:To create maintainable code, however, you should abstain from using “inline Javascript” (i.e.: code that’s directly within an element’s tag) and modify an element’s behavior via an included Javascript source file (it’s called unobtrusive Javascript).
The mark-up:
The code (separate file):