jquerypreventdefault

event.preventDefault() on first click then remove


I have the default anchor disabled if it has a class subnav as shown in this fiddle.

I only want this disabled for the first click then I want the normal anchor functionality to be brought back. What is the best way to do this? I tried something involving the below code but this didn't seem to work?

  $(this).unbind(event.preventDefault());

maybe something like this psuedo code?

if (click count === 0 ) {
    event.preventDefault();
}

or is there a better way to approach this?


Solution

  • You can pass false to one():

    $(".subnav a").one("click", false);
    

    Passing false instead of a handler is equivalent to passing a handler that returns false, effectively stopping the event's propagation and preventing its default behavior.

    This is explained in the documentation for bind():

    In jQuery 1.4.3 you can now pass in false in place of an event handler. This will bind an event handler equivalent to: function() { return false; }.