-1

I am trying to find if a class name exists in a webpage, and then add a new class.

Testing in Chrome Developer Tools Console I get undefined. What am I doing wrong?

Code I am using.

const srtClass = document.querySelectorAll('.sr-only'); 
srtClass.className += " showoffScreenText";
console.log(srtClass);

The results for the console.log

HTMLCollection(2) [span.sr-only, div.sr-only] className: "undefined showoffScreenText"

The sr-only class is found so why is it undefined?

Vega
  • 27,856
  • 27
  • 95
  • 103
Laurence L
  • 613
  • 1
  • 8
  • 21

1 Answers1

1

"querySelectorAll" returns a NodeList, you should get the first element

srtClass = document.querySelectorAll('.sr-only')[0]
de3
  • 1,890
  • 5
  • 24
  • 39