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

184
Visualizações
How to use .contains on childList mutation?

I am using MutationObserver and want to check a class selector on target.className but I get an error

Cannot read properties of undefined (reading 'contains')

so I looked at classList's prototype and it has .contains method and I did the following target.classList.prototype.contains("OverlayWrapper") and I still get the same error.

I don't understand why I am getting this error as I am trying to check for a partial className .

Edit: I have added a snapshot of the consoleenter image description here

Edit: I have made an example for the code, it is the bare minimum where I am looking for a class that contains popUp__wrapper being added to the DOM. (an error will be in the console when we press the open button) jsfiddle

about 4 years ago · Juan Pablo Isaza
1 Respostas
Responde à pergunta

0

Issues

  • mutation.addedNodes is an nodeList (collection of nodes). So obviously classList not exists in it.

  • Your class popUp__wrapper is suffixed with a an scxdxcfd. No class with exact name popUp__wrapper.

Solution

let wrapperEl = Array.from(mutation.addedNodes) // convert NodeList to array
      .filter(n => n.nodeType != Node.TEXT_NODE) // remove text nodes
      .find(n => n.matches('[class*=popUp__wrapper]')) // search for class

If you need to check popUp__wrapper available inside the node, use querySelector instead of matches

Demo

https://jsfiddle.net/aswinkumar863/qjeafxd8/20/

References

https://developer.mozilla.org/en-US/docs/Web/API/Element/matches

https://developer.mozilla.org/en-US/docs/Web/API/Text

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