jQuery object disabled event listener, how to detect when a html attribute changes
10,688
Solution 1
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.
Solution 2
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);
Related videos on Youtube
Author by
Tony Bogdanov
Updated on June 04, 2022Comments
-
Tony Bogdanov almost 2 years
I need to make an event listener for a
select
tag. It should be triggered any time the element becomesdisabled
orenabled
.Is this even possible?
P.S.:
onchange
does not detect disabling. -
Daniel Katz over 9 yearsIs it possible to turn it off/on according to my needs?
-
Anil over 9 yearsI don't remember (Sorry!), this was from a few years ago now, The github repo can be found here: github.com/darcyclarke/Watch.js - You should be able to look over the source code and see if there is a function or property to toggle it.