Currently, I am attempting to loop through several DIV elements, extract a numerical value from each DIV, and then based on that value matching a specific value in the JavaScript code, assign a particular CSS Class back to the original DIV.
This is the snippet of code I have been working on:
const getRating = document.getElementsByClassName('my-ratings');
let getRatingValues = [];
for(var i = 0; i < getRating.length; i++) {
getRatingValues += getRating[i].textContent;
if (getRatingValues == 5) {
getRating.classList.add('rating-5');
}
if (getRatingValues == 4) {
getRating.classList.add('rating-4');
}
if (getRatingValues == 3) {
getRating.classList.add('rating-3');
}
if (getRatingValues == 2) {
getRating.classList.add('rating-2');
}
if (getRatingValues == 1) {
getRating.classList.add('rating-1');
}
}
I feel like I am very close to solving this issue, but just can't quite figure it out...
Currently, it appears that the script is treating the values in getRatingValues as a string. For example, if I have values of 5, 3, 1, 2, 2, 4 in my DIVs, the getRatingValues will be '531224'. Can someone please help guide me in the right direction?