March 8, 2013

Is it possible to fire an event handler when the element is enabled or disabled?

Question by Rakesh Juyal

I want to execute a method when the input is enabled or disabled. Is it possible to do in jQuery?

I want to do something similar to this ( non-working code below ):-

$('input').on('enabled', function(e){
   alert( 'Just now someone enabled : ' + $(this).attr('id') );
   executeSomeHandlerForENABLED();
}).on('disabled', function(e){
   alert( 'Just now someone disabled : ' + $(this).attr('id') );
   executeSomeHandlerForDISABLED();
});

Answer by Starx

YES, but what you are trying is not an easy task.

What you can do is, use propertychange event (which by the way is not available on IE) to catch the change in properties.

I googled and found this solution implemented. A plugin to watch the property as it changes. For IE it fallbacks to setTimeout() which is very sad indeed.

Anyways, your solution utilizing that solution would be something like:

$("input").watch('disabled', function() {
    console.log($(this).is(":disabled"));
});

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!