Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

183
Views
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 answers
Answer question

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 Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!