I have a demo code here
I have a bar with separate blocks in it and a marker on top that can be positioned along the bar.When the marker is on top of a particular block I want to color that block.
This is working but I want each block to be a different color. I have a color class on each block that will work with 'selected' added to the end.The color class is like 'bar-item-yellow' Is it possible to add 'selected' to a class that contains 'bar-item-'
const marker = document.querySelector('.bar-marker')
const bars = document.querySelectorAll('.bar-item');
(function() {
for(let i=0; i<=bars.length; i++){
if(marker.offsetLeft >
bars[i].offsetLeft && marker.offsetLeft <
bars[i].offsetWidth+bars[i].offsetLeft){
bars[i].classList.add('bar-item--selected')
}
}
})();