Monday 15 March 2010

javascript - Reattach a removed event handler doesn't work -



javascript - Reattach a removed event handler doesn't work -

i want remove click of element when clicked (i'm using off() this), , want reattach click when element isn't clicked ('actived'). indicate not clicked, i'm using class named 'disabled'.

but when remove, cant add together again. doesn't attach event again!

this i'm trying do:

$('.my-element').on('click', function() { $('.my-element').off('click'); }); var disabled = setinterval(function() { if($('.my-element').hasclass('not-clicked')) { $('.my-element').on(); } }, 1000);

i'm using setinterval() watch whether element isn't clicked. if isn't, can using on() again.

i have tried remove event handler in browser console:

$('.my-element').off();

but when seek reattach event handler...

$('.my-element').on();

it doesn't work, , not repeat behavior.

using on , off without parameters has no effect. ideally should pass name of event , it's handler methods.

var handler = function() { // ... } $('.my-element').on('click.namespace', handler); // ... $('.my-element').off('click.namespace', handler);

if handler should called 1 time consider using one method. using setintevarl here bad idea. should bind handler right after adding class. in case i'd suggest using event delegation technique:

$('#astaticparent').on('click', '.my-element.not-clicked', function() { // handler called if element has `not-clicked` classname $(this).removeclass('not-clicked'); });

javascript jquery

No comments:

Post a Comment