Ensure that the background color of the radio buttons (Survey Scale CSS) changes after being selected, leaving only the chosen button with a different color

There is a problem with the radio buttons' background color not displaying correctly after being checked and for the unselected options. Here is the CSS code snippet:

/*main rating class*/

.rating {
  position: inherit;
  display: inline-flex;
  width: 100%;
  box-sizing: border-box;
  border: 1px solid #000;
  background: linear-gradient( to left, #f00, #ff0, #0f0);
}

.rating input {
  display: none;
}

.rating label {
  position: inherit;
  margin: inherit;
  display: flex;
  cursor: pointer;
  width: 100%;
  justify-content: center;
  align-items: center;
  transition: 0.5s;
  background: #fff;
  color: #000;
  border-right: 1px solid #000;
}

.rating:not(:checked)>label:hover,
.rating:not(:checked)>label:hover~label {
  background: transparent;
  cursor: pointer;
}

.rating>input:checked+label:hover,
.rating>label:hover~input:checked~label,
.rating>​input:checked~label:hover~label {
  background: transparent;
  cursor: pointer;
}
<div>
  <label for="rdbRating1">*1. XYZ </label>

  <!-- 1st Radio button list -->

  <div class="rating" id="rdbRating1">
    <input type="radio" value="7" id="7">
    <label for="7">7</label>
    <input type="radio" value="6" id="6">
    <label for="6">6</label>
    <input type="radio" value="5" id="5">
    <label for="5">5</label>
    <input type="radio" value="4" id="4">
    <label for="4">4</label>
    <input type="radio" value="3" id="3">
    <label for="3">3</label>
    <input type="radio" value="2" id="2">
    <label for="2">2</label>
    <input type="radio" value="1" id="1">
    <label for="1">1</label>
    <input type="radio" value="NA" id="NA">
    <label for="NA">NA</label>
  </div>
</div>

Answer №1

To achieve this, you can utilize vanilla Javascript...

If your goal is to select all elements that come after the clicked element within a line of elements, then you can implement a function that examines the next element in your nodeList from the selected element. This function can be incorporated within a forEach loop and eventListener.

Start by obtaining the element nodeList to iterate over using querySelectorAll(). Proceed to iterate over it using forEach(). Pass a parameter to capture the iterated value 'val', then establish an eventListener on that iterated value. Utilize the event object through the listener 'e' and employ the target property to pinpoint the clicked element.

Subsequently, utilize e.target to assign a class to the targeted element for styling.

You will also need to style the remaining siblings within the nodeList that belong to the LABEL category. For this purpose, leverage a helper function nextUntil() that fetches the remaining elements in the nodeList starting from the selected element triggered by the click event listener up to a specified target element within the nodeList.

Please note: I have inserted an empty span tag for selection in each of your rating child nodes.

To apply the desired class to the targeted elements, use e.target in conjunction with a for/in loop to ascertain if the nodeName matches 'LABEL'. Once all relevant labels are identified from the start to the targeted finish, apply the class across the selected nodeList.

// Your Javascript code snippet goes here
// Your CSS code snippet goes here
// Your HTML code snippet goes here

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

The daily scripture quote from the ourmanna.com API may occasionally fail to appear

I've been trying to display the daily verse from ourmanna.com API using a combination of HTML and JS code, but I'm encountering an issue where the verse doesn't always show up. I'm not sure if this problem is on the side of their API or ...

Conflict with iScroll

When using an HTML5 template in Mobile Safari on iPad, I encountered an issue with a div containing iScroll. Within this div, there is a jQuery function within an if/else statement which tests if the user is tapping outside of the iScroll div and fades it ...

Utilizing jQuery to pinpoint elements with personalized data attributes

I am struggling with a few elements that look like this <p data-lang-bg="Bulgarian" data-lang-en="English" >Text</p> <p data-lang-bg="Other bulgarian text" data-lang-en="Other english text" >Text 2</p> How can I write a jQuery sel ...

Reset the value of the input type file when the modal is closed and ensure that the Save All Changes button is deactivated

Hey there! Working on a simple project and trying to incorporate a neat trick in my app. I want to achieve the following: Clear the input type file value when the modal is closed. Essentially, if the user decides to cancel the upload and closes the modal, ...

Creating a dynamic and engaging full-width carousel slider using Bootstrap

I am currently utilizing the Mobirise Website Builder and am in the process of figuring out how to create a full-width Bootstrap carousel similar to the example shown at this link: My goal is to have the carousel at the top of the screen, covering only 50 ...

The carousel caption in Bootstrap 4 is not showing up on smaller screens

While experimenting with the Bootstrap 4 carousel example found at http://v3-beta.getbootstrap.com/components/#carousel, I encountered an issue where the carousel caption is not visible on mobile or small screens. Any assistance would be greatly apprecia ...

The jQuery.addClass() function seems to be malfunctioning

I'm encountering an issue with the addClass() method. For some reason, it's not functioning properly in this scenario: https://jsfiddle.net/0g1Lvk2j/20/ If you scroll to the end and close the last box by clicking on the orange box, the orange b ...

I am working with an array of image objects called imageList and I am trying to implement a specific design layout using flex

I have an array of objects called imageList, and I need to achieve a specific pattern using flexbox. How can we utilize flex-grow for the images? https://i.sstatic.net/RHqfK.jpg I have been able to create this pattern using grid (...pseudo(':nth-chil ...

Conceal a child div through the use of AJAX technology

Utilizing AJAX, I am loading pages on my website. Each page consists of two common div elements - the header div and the content div. When navigating from one page to another, I load the content div of the next page into the current page using the followin ...

I am having issues with my dropdown-list on my second script not functioning properly

Hello there! I'm new to this platform and English is not my strong suit, so bear with me. I've encountered an issue in my code that I need help with. Step 1: I successfully created the first dropdown list. Step 2: The second dropdown l ...

Transferring information from various HTML forms using JQuery's ajax functionality

Hello there! I have recently started learning how to use jQuery AJAX for file uploads. Currently, I am working with a simple HTML form and including JavaScript/jQuery code within it to make an AJAX request. <!DOCTYPE HTML> <html> <head> ...

The end of the page is not displayed fully in Safari

I've been scratching my head trying to understand why this Safari browser issue is happening on this page: . It seems to be working perfectly fine on all other browsers that I've tested so far! The code includes some jquery and css to adjust the ...

Using Ajax to fetch and display HTML and PHP pages

After successfully implementing an ajax function to load html pages inside an ajax-container div in my index.php, I am now wondering how I can also call php pages using ajax. Here is the code snippet that I currently have: htaccess rewrite Options +Fol ...

Having difficulty rotating images using the jQuery Rotate plugin?

I have successfully implemented a rotate plugin to rotate my logo on my website. However, I am facing an issue where the content below the rotating image is moving up and down. Could someone please advise me on how to correct this issue? You can view my ...

What is the output of the createRange() function?

I am encountering an issue with my code that is causing the highlighted text to always appear at the end of the page when it pops up. Is there a way to make it show in the middle of the line or below the selected text instead? I have included the current ...

Tips for incorporating jQuery to load Rails form partials and submit them as a single form:

Trying to improve the readability of my Rails HTML code, I decided to extract certain portions into partials and then used jQuery to render these partials. However, a problem arose where the form seems disconnected after implementing this approach. It appe ...

Improprove the performance of external banner ads by utilizing asynchronous loading or caching techniques

My website is slick, elegant, and loads quickly - that is until the banner ads come into play! A delay of up to 10 seconds in loading occurs due to waiting for the ads, which is frustrating considering the effort I put into optimizing the rest of the site. ...

Conceal email address within HTML code

Similar Inquiry: What is the best way to hide an email address on a website? I've been contemplating ways to keep my email address secure on my website, including using AJAX to request it from the server after key validation and then simulating H ...

How can Highcharts load pointStart from JSON and convert milliseconds to UTC time?

I created a Highcharts graph with data loaded from a JSON file. Handling JSON files is new to me, but I am learning. I want the Highcharts graph to display dates in the format YYYY/MM/DD (e.g., 2013/04/01) on the xAxis labels. Since I couldn't direc ...

Retrieve the next element in android studio that shares the same class name as jsoup

i am trying to access the next element with the same class name in HTML. The HTML tag structure looks like this: HTML: <section class="post"> <img class="pecintakomik" src="/images/top/op.jpg" alt="pecintakomik.com" /> <div class=" ...