0

Hi new to Javascript here so please be gentle. I want to write some js code that detects changes in an element's attribute. tia and any help is appreciated.

my code reads likes this:

$("#targetElement").attr('testAttr').change(function(){
    //some code to do stuff
});
Jane Alice
  • 91
  • 9
  • This doesn't work like this. What kind of element is #targetElement? Is it a form element? – Stephan Vierkant Jul 28 '21 at 18:29
  • This is not how `change` event handlers work. To achieve what you require you would need to raise an event manually when you change the attribute value, or use a MutationObserver. Details of both are in the duplicate in my comment above. Also note that creating your own non-standard attributes can lead to some unexpected issues. I would strongly suggest you convert the `testAttr` attribute in your HTML to a [data](https://developer.mozilla.org/en-US/docs/Learn/HTML/Howto/Use_data_attributes) attribute to avoid that problem – Rory McCrossan Jul 28 '21 at 18:29
  • Stephan, it is a text box element. – Jane Alice Jul 28 '21 at 18:33

0 Answers0