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

89
Views
Javascript if svg has role add class

I'm trying to add a class to an SVG if the does not have a role="img".

I can add a class to the SVG with role img

const imgRole = document.querySelectorAll('svg[role="img"]');

imgRole.forEach(function(el) {
  el.classList.add("my--class");
});

However I can't target the SVGs that do not have the role.

I tried to find a way of using not !, but had no success.

if (!el.classList.contains...

I'd appreciate any help in showing me how to achieve this.

about 4 years ago · Santiago Trujillo
1 answers
Answer question

0

Try using :not

document.querySelectorAll('p:not([role=a])').forEach(p => p.style.color = 'red')
<p role="a">lorem ipsum</p>
<p role="not a">lorem ipsum</p>
<p role="a">lorem ipsum</p>
<p role="also not a">lorem ipsum</p>

In your case:

'svg:not([role="img"])'
about 4 years ago · Santiago Trujillo 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!