Click here to Skip to main content
15,885,767 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
In the video I have lines which contain divs and those divs change color on how they are selected. For example when I have selected the box on extreme right then all boxes get colored in dark green. If I select one in the middle, all of the divs get colored in yellow but not the ones after it. How can I achieve such functionality?

Link to the image: NPS.webm - Google Drive[^]

What I have tried:

I tried to use for each loop and then adding color on click but it didn't work out.
Posted
Updated 14-Jul-21 21:26pm
Comments
Richard MacCutchan 13-Jul-21 11:12am    
"How can I achieve such functionality?"
Probably by writing some code, or correcting what you already have in your secret source.
Arun Bohra 2021 13-Jul-21 14:41pm    
Here's the link to my code in codepen.
Please I need this.

https://codepen.io/ArunBohra33/pen/ZEKLOWd
Richard MacCutchan 14-Jul-21 3:54am    
Please edit your question and add all the relevant details here. I, and most other people here, am not going to some other website in order to try and figure out what your code does.
Richard Deeming 14-Jul-21 4:35am    
There seems to be a problem posting answers at the moment. Here's a quick Demo[^] - I'll post a proper solution once the site is working again.

1 solution

Here's how I'd do it:

HTML:
HTML
<ul class="responsive-review">
    <li style="--rating-color:darkred">
        <input type="radio" name="rating" id="rating-1" value="1" />
        <label for="rating-1"></label>
    </li>
    <li style="--rating-color:orangered">
        <input type="radio" name="rating" id="rating-2" value="2" />
        <label for="rating-2"></label>
    </li>
    <li style="--rating-color:yellow">
        <input type="radio" name="rating" id="rating-3" value="3" />
        <label for="rating-3"></label>
    </li>
    <li style="--rating-color:yellowgreen">
        <input type="radio" name="rating" id="rating-4" value="4" />
        <label for="rating-4"></label>
    </li>
    <li style="--rating-color:darkgreen">
        <input type="radio" name="rating" id="rating-5" value="5" />
        <label for="rating-5"></label>
    </li>
</ul>
CSS:
CSS
.responsive-review {
    list-style: none;
    display: inline-flex;
}
.responsive-review > li {
    margin: 0 5px;
    position: relative;
}
.responsive-review input[type='radio']{
    position: absolute;
    top: 0; left: 0; right: 0; bottom: 0;
    opacity: 0;
}
.responsive-review label {
    display: block;
    width: 50px;
    height: 40px;
    background-color: #bbb;
}
.responsive-review input[type='radio']:checked + label{
    background-color: var(--rating-color);
}
Javascript:
JavaScript
const updateReview = function(evt) {
    const selectedLi = evt.currentTarget;
    const ratingColor = getComputedStyle(selectedLi).getPropertyValue("--rating-color");

    for (let li = selectedLi.previousElementSibling; li; li = li.previousElementSibling) {
        const label = li.querySelector("label");
        label.style.backgroundColor = ratingColor;
    }

    for (let li = selectedLi; li; li = li.nextElementSibling) {
        const label = li.querySelector("label");
        label.style.backgroundColor = null;
    }
};

const lists = document.querySelectorAll(".responsive-review > li");
lists.forEach(li => li.addEventListener("click", updateReview));
Demo[^]

The <input type="radio"> handles selecting a single item from the list.

The CSS automatically assigns the correct background colour to the selected item, using CSS variables on the list items to define the colour for each.

You just need a little bit of Javascript to set the correct background colour on the previous list items, and clear the explicit background colour from the selected item and any following items.

You'll probably want to add some text to the labels for accessibility, possibly hidden for anything other than screen readers. And remember to check that your chosen colours work for colour-blind users.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900