Search code examples
javascriptjqueryjquery-events

e.preventDefault - is there a way to doDefault?


Let's say I have a link:

<a href='http://example.com/file.zip' id='dl'>Download</a>

And while someone click on it, I want to do something, such as:

$("#dl").live("click", function(e){
    e.preventDefault();
    $("#dlbox").fadeOut(500);
});

Is there a way after I e.preventDefault, to go ahead and doDefault? something like:

$("#dl").live("click", function(e){
    e.preventDefault();
    $("#dlbox").fadeOut(500);
    e.doDefault();
});

so it'll run the event normally?


Solution

  • You can only prevent the default behavior (with e.preventDefault()).

    Now if you expect the link to be navigated when the .fadeOut() method has finished, you should prevent the default behavior and use the callback parameter of the fadeOut method to navigate when the animation has finished:

    $("#dl").live("click", function(e){
        e.preventDefault();
        var href = this.href;
        $("#dlbox").fadeOut(500, function() {
            window.location = href;
        });
    });
    

    DEMO