-1

Once preventDefault(); is called, how can I invoke that event that was just prevented?

For example:

$("a").click(function(e){

    setTimeout(function(){
        e.huh? // run original function
    },5e3);
});
Brian Tompsett - 汤莱恩
  • 5,753
  • 72
  • 57
  • 129
d-_-b
  • 21,536
  • 40
  • 150
  • 256
  • *this should be an easy one* Not really; once the default is prevented, you can't invoke it. But what do you mean by "original function"? The default for an anchor click is to follow the link, is that what you're trying to do? – bfavaretto Aug 13 '13 at 22:37
  • @bfavaretto not necessarily, there can be other events bound to it. One option i can think of would be to copy the original function and then run that, but i'm looking to see if there's a better way. – d-_-b Aug 13 '13 at 22:38
  • 2
    If you have multiple event handlers for the same element, they will all be triggered, regardless of preventing the default or not. If you have handlers on ancestors, they will also trigger, unless you stop event propagation. Maybe that's what you're asking about? – bfavaretto Aug 13 '13 at 22:40
  • 1
    This is a duplicate of many different ones. Just google "Restore default after preventDefault()". @d-_-b – Zack Argyle Aug 13 '13 at 22:40
  • What about a way to copy the functions that would have been called - I guess my question is, can you copy an element/function's bindings? @ZackArgyle can you provide links along with your helpful comment. – d-_-b Aug 13 '13 at 22:42

1 Answers1

1

This will work

$("a").one("click", function (e) {
    e.preventDefault();
    var elem = this;
    setTimeout(function () {
        elem.click();
    }, 2000);
});

http://jsfiddle.net/XyETg/3/

Esailija
  • 138,174
  • 23
  • 272
  • 326
  • This will not make a real click event. – jack Aug 13 '13 at 22:46
  • On the contratry @jack, this *will* make a real click event. jQuery is the one "faking" it. – bfavaretto Aug 13 '13 at 22:47
  • No it's not. Binding another listener calling window.open will fail when *default* is imitated. – jack Aug 13 '13 at 22:57
  • 1
    @jack yes the `elem.click()` will not be considered to originate from user action even if the actual event happened to, I think that is irrelevant here since the intention is to just follow the link which works fine. – Esailija Aug 13 '13 at 22:58