7

event.preventDefault() will override default event behavior of an element. How can I temporarily override all click bindings and not just default ones? Or is there a way to save all the click bindings so I can unbind them and use them later?

Well this is not a proper answer but a workaround. We can push the required handler on top of the stack and then used return false to stop other bindings. https://github.com/private-face/jquery.bind-first

Lokesh Suthar
  • 3,201
  • 1
  • 16
  • 28
  • for all events binded with jquery, see $._data(elem,'events').click where elem is a DOM element, not a jquery object e.g to give you the idea http://jsfiddle.net/5tG2w/ – A. Wolff Jun 14 '13 at 10:39
  • I tried this but I am not sure if that will provide all the bindings including external bindings through .click(), .bind(), or .on(), would it ? – Lokesh Suthar Jun 14 '13 at 21:18

3 Answers3

2

You can use jQuery.clone(true) what this does is return data for an element. The parameter that is set to true means to also copy over all the events as well.

So if you clone the element into a variable you can bring back the old click events by simply replacing your target element with its older clone (which has the old events)

So it goes as follows:

step 1: clone the target element using jQuery.clone(true) into a variable

step 2: remove all click events from the target element using jQuery.off('click')

step 3: bind your event to the target element with jQuery.on('click' function etc...)

step 4: when you're done replace the target element with its clone (which has the old events)

Here is a JSFiddle for your viewing pleasure

(Sorry for the simpleness of the JSFiddle I mocked it up quickly and I have no example situation where I would use this.)

EDIT: I forgot to explain jQuery.clone(true)

Michael
  • 410
  • 4
  • 9
1

You may catch the click before it can bubble by using

element.addEventListener(type, listener[, useCapture]);

This way you can 'catch' the click before triggering the jQuery click handler, like this (which I took from this stackoverflow question:

document.addEventListener('click', function(e) {
    e.stopPropagation();
}, true);

For more information (and some IE < 9 support), see developer.mozilla

Edit: details about useCapture from Mozilla:

If true, useCapture indicates that the user wishes to initiate capture. After initiating capture, all events of the specified type will be dispatched to the registered listener before being dispatched to any EventTarget beneath it in the DOM tree. Events which are bubbling upward through the tree will not trigger a listener designated to use capture. See DOM Level 3 Events for a detailed explanation. If not specified, useCapture defaults to false.

Community
  • 1
  • 1
Severin
  • 381
  • 1
  • 3
0

If you have control over all of the JS code and can bind your own handler first and all other event handlers are bound with jQuery then you can do this:

var overrideClick = false;
$("#yourElementId").click(function(e) {
    if (overrideClick) {
        e.stopImmediatePropagation();
        // e.preventDefault();    uncomment this if you want to prevent default action too
    }
});

Where some other part of your code would set overrideClick = true when needed.

Demo: http://jsfiddle.net/NCa5X/

jQuery calls handlers in the order they are bound, so you can then use event.stopImmediatePropagation() to prevent the other handlers from being called.

nnnnnn
  • 147,572
  • 30
  • 200
  • 241
  • Thank you for your reply, but unfortunately I don't have full control. – Lokesh Suthar Jun 14 '13 at 21:14
  • I just spent some time reading through the event handling jQuery uses and I think you could write a plugin to do this for you. Will definitely take some time and reading but if I understand it correctly, jQuery implements its down event handling and has complete control over the stack of handlers. https://github.com/jquery/jquery/blob/master/src/event.js – travis Jun 14 '13 at 23:23
  • @travis - Note that a jQuery plugin is only going to cope with event handlers bound with jQuery. If there is a mix of code that the OP can't control then other scripts may have used `addEventListener()` and you wouldn't be able to do anything with those handlers. – nnnnnn Jun 15 '13 at 04:32
  • If they are bound with prototype, perhaps you could write a jQuery plugin that calls prototype for added coverage. – bean5 Oct 20 '13 at 15:20