Search

Dark theme | Light theme

November 17, 2008

Prevent href follow on anchor element in YUI

I am using the YUI library for a project. I have written some code that invokes a Javascript function when a user clicks on an anchor element. But I don't want the href to be followed after the Javascript function has finished. After some googling I found this can be achieved with the YAHOO.util.Event.preventDefault(event); statement.

<a href="#" id="mylink">Click me</>
function clickCallback(event) {
  YAHOO.util.Event.preventDefault(event);
  alert('You clicked me');
}
YAHOO.util.Event.addListener("mylink", "click", clickCallback);