0

How can I use .preventDefault() for every event below? Do I have to specify it for each event or can I do it another way?

$("h3 a").on({
click: function(){
    console.log("This is a click event");
},
mouseenter: function(){
    console.log("This is a mouseenter event");
},
mouseleave: function(){
    console.log("This is a mouseleave event");
},
dblclick: function(){
    console.log("This is a double click event");
}, function(e){
        e.preventDefault(); // not working
    }
});
3
  • 3
    You have to specify for each event. Unless you have same handling code for each of those events, if yes.. then you could have it all in one function. Commented Oct 29, 2013 at 18:55
  • @Vega: Does that mean I can use: $("h3 a").on("click mouseenter mouseleave dblclick", function(e){ e.preventDefault();}); ? Commented Oct 29, 2013 at 19:00
  • Yes, only if the implementation is same for all those events. Commented Oct 29, 2013 at 19:01

2 Answers 2

4

One approach:

$('a').on('click mouseenter mouseleave dblclick', function (e) {
    e.preventDefault();
}).on({
    click: function () {
        console.log("This is a click event");
    },
    mouseenter: function () {
        console.log("This is a mouseenter event");
    },
    mouseleave: function () {
        console.log("This is a mouseleave event");
    },
    dblclick: function () {
        console.log("This is a double click event");
    }
});

JS Fiddle demo.

Though this works, it does feel rather verbose and, it would, probably, make sense to try and handle all the functionality in one common event-handler, perhaps triggering/calling different functions based on a switch statement, for example:

$('a').on('click mouseenter mouseleave dblclick', function (e) {
    e.preventDefault();
    switch (e.type) {
        case 'click':
            clickFunction(e);
            break;
        case 'mouseenter':
            mouseenterFunction(e);
            break;
            /* ...and so on... */
    }
});

JS Fiddle demo.

Incidentally, JavaScript doesn't really distinguish between a click and a dblclick event, unless you measure the time between click events youself; so your event-handling is likely to be problematic.

Sign up to request clarification or add additional context in comments.

4 Comments

This is neat. I never though of binding another click event like that.
I agree and it is one way but I would rather have it in all instead of adding a new handler.. but it could be just me.
@Vega, indeed: hence my second suggestion, which is basically the same as your own (though a coincidence, I promise...)
@DavidThomas haha.. my +1 for having both suggestions.
2

Another way is to use e.type inside a common handler.

$('a').on('click mouseenter mouseleave dblclick', function (e) {
    e.preventDefault();
    switch(e.type) {
      case 'click': 
        console.log("This is a click event");
        break;
      case 'mouseenter':
        console.log("This is a mouseenter event");
        break;
      case 'mouseleave':
        console.log("This is a mouseleave event");
        break;
      case 'dblclick': 
        console.log("This is a double click event");
    }
});

I still don't recommend the above code, but just posting it in here as an alternate option.

My suggestion is to have the e.preventDefault() in each of the handler.

2 Comments

I will end up using e.preventDefault() in each handler, I was just curious. Thank you anyway.
And a reciprocated +1 for the same suggestion. :)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.