classList | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
0

classList

I am trying to make when u tap on either the red or orange text, the class "yellow" will be implied. But there is an error that I don't know how to get around. https://code.sololearn.com/WsnAuVPS64uk/?ref=app

7th Mar 2022, 7:44 AM
WolfieElite
WolfieElite - avatar
2 Answers
+ 2
document.getElementsByClassName() method returns a collection, there can be none, only one, or multiple items in the collection. All items are references to elements having the classname given as argument to document.getElementsByClassName(). So, if you only have one element with each class, I guess you better refer to the element by its `id`, using document.getElementById().
7th Mar 2022, 7:55 AM
Ipang
+ 1
document.getElementByClassName("nameOfClass") return array, so to access data you use index redTex[0].classList.add("yellow") If you will have only 1, then use id, but if you plan to have more, then you will mostly use loop, to set something to every element of this array. Accessing wia index is not good if you have list of 100 elements with same class name.
7th Mar 2022, 9:06 AM
PanicS
PanicS - avatar