Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

195
Visualizações
MutationObserver: new value in the format of `oldValue`

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 than mutation.oldValue as well. For details, see here.
about 4 years ago · Juan Pablo Isaza
1 Respostas
Responde à pergunta

0

Attributes and properties are different things. To get the attribute string, you'd use getAttribute:

newValue = mutation.target.getAttribute(mutation.attributeName);

You were accessing the property from the element instance, which in the case of style is a CSSStyleDeclaration object reflecting the parsed style properties from the style attribute. While many attributes are reflected directly as properties (id, name), others are in a different form (like style) or aren't available as properties on the element at all.

about 4 years ago · Juan Pablo Isaza Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda