I'm using a MutationObserver
and I'd like to access the new value of the attibute. I try this:
newValue = mutation.target[mutation.attributeName]
However, if for example mutation.attributeName == 'style'
, then mutation.oldValue
and my newValue
are formatted differently: mutation.oldValue
is a string formatted like newValue.cssText
, whereas newValue
itself is a CSSStyleDeclaration
object.
I would like to avoid programming for each possible mutation.attributeName
things like "if 'style'
, use .cssText
, if ..., use ...". There are too many possible attributes.
How do I get the new value in the format of oldValue
?
Related questions don't answer my question:
mutation["addedNodes"][0]
from here is undefined in my case.- This answer requires the element to have an ID. That might be difficult to achieve in some cases.
Edit:
- This answer suggests
newValue = mutation.target.attributes.getNamedItem(mutation.attributeName)
, but that has a different format thanmutation.oldValue
as well. For details, see here.