You can use an onClick event but if I remember correctly, you must return false in order to prevent your page from jumping around; something like:
or: provided that
myfunction
will return false.
You can also directly call a javascript from href but you must cast the result to void in order to block to the browser to try to follow the result as a valid link:
Even if you still want to use the onClick property; it would still be a good idea to replace the href="#"
with href="javascript:void(0)" ...>
.
Other people have mentionned using the event.preventDefault()
and stopPropagation()
. I don't remember ever using one of these but I must admit that it has been many years since the last time that I have coding some javascript in a HTML link; so you should definitely investigate the use of these two functions.
EDIT: maybe that using a href="javascript:void(0)"
could be a bad idea sometimes; see http://drupal.org/node/1193068 .