1
<textarea id="test" disabled>Test</textarea>

Is there a way to listen to the changes in the attributes? such as disabled attribute.

I need an event to add or remove "disabled" attr. Something like this:

$("#test").on("change attribute",function(){
    alert("Textarea enabled");
})
1
  • 1
    Yes, look at MutationObserver. Commented Nov 29, 2020 at 15:57

1 Answer 1

2

You could use a MutationObserver:

console.log('script start');
const test = document.querySelector('#test');

new MutationObserver((mutations) => {
  for (const mutation of mutations) {
    if (mutation.attributeName === 'disabled') {
      console.log('Change detected');
    }
  }
})
  .observe(test, { attributes: true });

setTimeout(() => {
  test.removeAttribute('disabled');
}, 1000);
<textarea id="test" disabled>Test</textarea>

No external libraries like jQuery needed; this is supported in essentially all browsers nowadays.

Sign up to request clarification or add additional context in comments.

2 Comments

Thanks for this. I am testing it
That was perfect.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.