June 14, 2013

Is there a way to temporarily override all click bindings on an element using jQuery?

Lokesh Suthar’s Question:

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?

Simply as this

$("*").on('click', function(e) {
    //override
    e.stopPropagation(); // To stop event bubbling further
});

To make this work temporarily, you can add a flag like

var override = true;

Now, check this variable before overriding the

$("*").on('click', function() {
    if(override) { 
         //override
         e.stopPropagation();
    }
});

Author: Nabin Nepal (Starx)

Hello, I am Nabin Nepal and you can call me Starx. This is my blog where write about my life and my involvements. I am a Software Developer, A Cyclist and a Realist. I hope you will find my blog interesting. Follow me on Google+

...

Please fill the form - I will response as fast as I can!