Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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?

like image 210
Itai Sagi Avatar asked Mar 05 '12 12:03

Itai Sagi


People also ask

How do you reverse e preventDefault?

There is no opposite method of event. preventDefault() to understand why you first have to look into what event. preventDefault() does when you call it. Underneath the hood, the functionality for preventDefault is essentially calling a return false which halts any further execution.

Why e preventDefault () is used?

The preventDefault() method is used to prevent the browser from executing the default action of the selected element. It can prevent the user from processing the request by clicking the link. Parameters: It does not accept any parameter.

How is preventDefault () Used with drag and drop?

In a web page, you should call the preventDefault() method of the event if you have accepted the drop, so that the browser's default handling is not triggered by the dropped data as well. For example, when a link is dragged to a web page, Firefox will open the link.

What is the difference between event preventDefault and return false?

preventDefault() prevents the default browser behavior for a given element. stopPropagation() stops an event from bubbling or propagating up the DOM tree. Whereas, return false is a combination of both preventDefault() and stopPropagation() .


2 Answers

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

like image 146
Didier Ghys Avatar answered Oct 08 '22 14:10

Didier Ghys


There is no doDefault() command, you need to re-architect your code.

I would prevent default, then perform the url direct yourself via window.location. Possibly use a different tag to an anchor tag too - by overriding its functionality it makes no sense to use it in the first place.

eg something like this:

$("#dl").live("click", function(e){
    e.preventDefault();
    $("#dlbox").fadeOut(500, function() { window.location='www.fish.com'; });
});
like image 20
NibblyPig Avatar answered Oct 08 '22 14:10

NibblyPig