0

I want to add a class (underline) to each element with rel_row_0 class.

I have this code:

<a onclick="document.getElementsByClassName('rel_row_0').classList.add('underline');">X</a>

I having this error message: Uncaught TypeError: Cannot read properties of undefined (reading 'add') at HTMLAnchorElement.onclick

Usually I don't use javascript so i'm not that good at it.

Thanks for all the help!

Roland
  • 1
  • 2
  • 1
    `document.getElementsByClassName` returns a node list. A node list does not have the `classList` property. – Terry Jan 09 '22 at 12:47
  • document.getElementsByClassName('rel_row_0') should return an array. You have to do something like document.getElementsByClassName('rel_row_0')[0] if you have only one element with that class name – strek Jan 09 '22 at 12:47
  • @strek What if I have more than one elements with that class name? – Roland Jan 09 '22 at 12:50
  • You would have to do a forEach for all elements and add class name – strek Jan 10 '22 at 14:35

0 Answers0