jquery - Universal selector .on event handles does not bind? -


just little problem popping up:

$("*").on("click", function(event) { if ($$(event.target).hasclass("disabled")) {     event.preventdefault();     event.stoppropagation();     return;  } }); 

is supposed add event every element, which, if element has class 'disabled', stopping click event being handled other eventhandler.

i thought simple , basic application, seems working on links, a-tag-elements, not on normal elements, in case keep handling click events.

i've tried few variations, return false, stopimmediatepropagation etc, same result. missing? matter 'where' in script add event handler? there execution order bound event handlers or something?

there couple problems you're trying do.

first, code attach event handler elements already on page, since elements jquery can select $("*") collection.

second, mentioned in comments, code attach event handler every element on page, absurdly slow depending on how many elements on particular page.

third, event handlers executed in order based on element attached (child element handlers executed before parent event handlers) , in order bound.

i can suggest 3 possible ways of doing this, depending on you're trying accomplish , how of code you're willing change.

if you're trying make links , buttons disabled not work, use delegated event handling disable default browser functionality.

$(document).on("click", "a, button", function(e) {     e.preventdefault(); }); 

if you're trying disable custom functionality you've added page, might better add global variable checked other functions. require changing lot of different pieces of code, isn't feasible, might nice able turn off page var doanything = false;. keep in mind if choose way of doing things client able turn functionality on console.

the third way solve add :not(.disabled) each selector bind click events.

if change this

$(".add").click(function(e) {     // }); 

to this

$(".add:not(.disabled)").click(function(e) {     // }); 

then work also.


Comments

Popular posts from this blog

android - Get AccessToken using signpost OAuth without opening a browser (Two legged Oauth) -

org.mockito.exceptions.misusing.InvalidUseOfMatchersException: mockito -

google shop client API returns 400 bad request error while adding an item -