114

Is it possible in JavaScript to listen for a change of attribute value? For example:

var element=document.querySelector('…');
element.addEventListener( ? ,doit,false);

element.setAttribute('something','whatever');

function doit() {

}

I would like to respond to any change in the something attribute.

I have read up on the MutationObserver object, as well as alternatives to that (including the one which uses animation events). As far as I can tell, they are about changes to the actual DOM. I’m more interested in attribute changes to a particular DOM element, so I don’t think that’s it. Certainly in my experimenting it doesn’t seem to work.

I would like to do this without jQuery.

Thanks

1
  • 3
    MutationObserver works for that. Just configure it to listen to attribute changes. Set it to observe only the element you’re interested in. Jan 2, 2017 at 10:27

2 Answers 2

194

You need MutationObserver, Here in snippet I have used setTimeout to simulate modifying attribute

var element = document.querySelector('#test');
setTimeout(function() {
  element.setAttribute('data-text', 'whatever');
}, 5000)

var observer = new MutationObserver(function(mutations) {
  mutations.forEach(function(mutation) {
    if (mutation.type === "attributes") {
      console.log("attributes changed");

      // Example of accessing the element for which 
      // event was triggered
      mutation.target.textContent = "Attribute of the element changed";
    }
    
    console.log(mutation.target);
  });
});

observer.observe(element, {
  attributes: true //configure it to listen to attribute changes
});
<div id="test">Dummy Text</div>

Additionally, mutation.target property gives the reference to mutated/changed node.

4
  • Thanks for the answer and for the sample. Once I have streamlined this, I think it will be good technique for making more complex changes to elements by simply changing and attribute. It makes an easier API. Would you be aware of a reasonable polyfill for primitive browsers?
    – Manngo
    Jan 2, 2017 at 10:44
  • @Manngo, For primitive browsers you can use Mutation events and also see davidwalsh.name/dom-events-javascript
    – Satpal
    Jan 2, 2017 at 10:46
  • I see that it is currently supported back to IE9, and still supported in modern browsers. Thanks again.
    – Manngo
    Jan 2, 2017 at 10:49
  • kinda useless when we have no access to the element in the observer
    – inux
    Dec 3, 2022 at 17:11
9

This question is already answered, but I'd like to share my experiences, because the mutation observer did not bring me the insights in needed.

Note This is some kind of hacky solution, but for (at least) debugging purposes quite good.

You can override the setAttribute function of a particalar element. This way you can also print the callstack, and get an insight of "who" changed the attribute value:

// select the target element
const target = document.querySelector("#element");
// store the original setAttribute reference
const setAttribute = target.setAttribute;
// override setAttribte
target.setAttribute = (key: string, value: string) => {
  console.trace("--trace");
  // use call, to set the context and prevent illegal invocation errors
  setAttribute.call(target, key, value); 
};
1
  • 1
    A clever hack. Note that if setAttributeNS() is called this will fail.
    – Phrogz
    Mar 11 at 17:48

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.