3

I'm looking to make changes on nearby elements when an input or fieldset is disabled. Is there an event that listens to when an element is enabled or disabled? For example:

var input = document.createElement('input')

I'm looking for an event that would fire from toggling the disabled state:

input.disabled = !input.disabled
Brian Tompsett - 汤莱恩
  • 5,753
  • 72
  • 57
  • 129
ryanve
  • 50,076
  • 30
  • 102
  • 137

1 Answers1

10

You can use MutationObserver with attributes set to true at configuration object.

var input = document.createElement("input");

var observer = new MutationObserver(function(mutations) {
  mutations.forEach(function(mutation) {
    if (mutation.attributeName === "disabled") {
      console.log(`${mutation.target.tagName}.disabled:`
                 , `${mutation.target[mutation.attributeName]}`)
    }
  });
});

observer.observe(input, {
  attributes: true
});

input.disabled = !(input.disabled); // true

setTimeout(function() {
  input.disabled = !(input.disabled); // false
});
guest271314
  • 1
  • 15
  • 104
  • 177