Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

jQuery object disabled event listener, how to detect when a html attribute changes

I need to make an event listener for a select tag. It should be triggered any time the element becomes disabled or enabled.

Is this even possible?

P.S.: onchange does not detect disabling.

like image 795
Tony Bogdanov Avatar asked Dec 09 '22 04:12

Tony Bogdanov


2 Answers

JQuery 1.6+

You can change jQuery prophooks:

jQuery.propHooks.disabled = {
  set: function (el, value) {
    if (el.disabled !== value) {
      el.disabled = value;
      value && $(el).trigger('disabledSet');
      !value && $(el).trigger('enabledSet');
    }
  }
};

This will trigger "disabledSet" if element is disabled via:

$(element).prop('disabled', true);

and will trigger "enabledSet" if element is enabled via:

$(element).prop('disabled', false);

Then you can just listen to your event like this:

$(element).on('disabledSet', myDisabledHandler);
like image 87
Olav Kokovkin Avatar answered Dec 31 '22 12:12

Olav Kokovkin


It is possible using this jquery .watch plugin.

http://darcyclarke.me/development/detect-attribute-changes-with-jquery/

The plugin detects for css changes, but i edited it in this JSfiddle to work for attributes.

Check out this JSfiddle.

like image 34
Anil Avatar answered Dec 31 '22 13:12

Anil