Modify radio button colors upon selection with a variety of colors

Looking to create Yes/No buttons that start with a default state of null and change color when pressed - green for yes, red for no.

Check out this example code:

<label class="formheading">Apparatus group is correct</label>
<fieldset data-role="controlgroup" data-type="horizontal" data-mini="false" class="right">
    <input type="radio" name="q2" id="q2y" value="Yes">
    <label for="q2y">Yes</label>
    <input type="radio" name="q2" id="q2n" value="No">
    <label for="q2n">No</label>
</fieldset>

Here's my jsfiddle - currently the button turns green for all selections, but I want it to turn red when "No" is clicked.

I've come across some similar solutions, however none seem compatible with JQM 1.4.2

Answer №1

To style the "No" label differently, you can simply add the class .no:

<label for="q2n" class="no">No</label>

Next, include this CSS rule:

.myform .ui-btn-active.no {
    background-color: red !important;
    border-color: crimson !important;
    color: #fff !important;
    text-shadow: none !important;
}

By doing this, you'll be able to easily add more buttons in the future if necessary.

Check out the demo here: http://jsfiddle.net/ambvdznb/6/

Answer №2

If you only need to include two buttons and nothing more, this solution worked perfectly for my needs.

.myform .ui-btn-active.ui-last-child{
    background-color: red !important;
    border-color: red !important;
}

This code specifically targets the last active button within each form with the class ".myform". No additional markup or classes are necessary in your HTML! :)

JFIDDLE: http://jsfiddle.net/ambvdznb/2/

Answer №3

To achieve the desired effect, simply include an additional selector for the specific number you want to target.

.myform .ui-btn-active[for="q2n"] {
    background-color: red !important;
    border-color: red !important;
}

For a visual demonstration, check out this example http://jsfiddle.net/ambvdznb/

Answer №4

It is recommended to apply the background-color property to the <label> element instead of the <input> element.

To begin, assign two unique IDs to your label elements:

...
<label for="q2y" id="q2yl">Yes</label>
...
<label for="q2n" id="q2nl">No</label>

Next, update your CSS to style the new elements accordingly:

.myform #q2yl.ui-btn-active {
    background-color: green !important;
}
.myform #q2nl.ui-btn-active {
    background-color: red !important;
}

You can then remove any existing background-color styling from the previous element.

.myform .ui-btn-active {
    border-color: #1F802E !important;
    color: #fff !important;
    text-shadow: none !important;
}

Answer №5

To differentiate between the 'Yes' and 'No' buttons on your form inputs, you can assign them classes such as 'yesbutton' and 'nobutton' respectively. Then, using a simple CSS-selector, you can style them with different colors like shown below:

//html

<div class="myform">
    <label class="formheading">Question 2</label>

    <fieldset data-role="controlgroup" data-type="horizontal" data-mini="false" class="right">
        <input type="radio" name="q2" id="q2y" value="Yes">
        <label for="q2y" class="yesbutton">Yes</label>
        <input type="radio" name="q2" id="q2n" value="No">
        <label for="q2n" class="nobutton">No</label>
    </fieldset>
</div>

//css

.right {
     float: right !important;
}
.myform {
    padding:20px;
}
.myform .ui-btn-active.yesbutton {
    background-color: green !important;
    border-color: #1F802E !important;
    color: #fff !important;
    text-shadow: none !important;
}
.myform .ui-btn-active.nobutton {
    background-color: red !important;
    border-color: #1F802E !important;
    color: #fff !important;
    text-shadow: none !important;
}

Answer №6

Fiddle

Take a look at this alternative solution.. :)

.myform .ui-first-child.ui-btn-active {
background-color: blue !important;
border-color: #2E1F80 !important;
color: #000 !important;
text-shadow: none !important;
}
.myform .ui-last-child.ui-btn-active {
background-color: yellow !important;
border-color: yellow !important;
color: #000 !important;
text-shadow: none !important;
}

Answer №7

Consider incorporating the following CSS code:

.myform .ui-btn-active[for=q2n] {
border-color: blue !important;
}

See Example on Jsfiddle

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

Connecting two divs with lines in Angular can be achieved by using SVG elements such as

* Tournament Brackets Tree Web Page In the process of developing a responsive tournament brackets tree web page. * Connection Challenge I am facing an issue where I need to connect each bracket, represented by individual divs, with decorative lines linki ...

Having trouble getting dynamic values to render properly in Ionic select? Find a solution in Ionic 4

Encountering an issue with Ionic 4 and ion-select. The problem arises when attempting to bind data, specifically when preselecting data. In this scenario, the ion-select element fails to render properly. <ion-item class="transparent"> ...

Creating a fresh array by applying a filter and assigning keys

I am facing an issue with my array structure, it looks like this, [ [ "Show/Hide", "P000", "MAX-CT05 FVM2-", "S", 1532, -9.5929406005, null, null, ...

Retrieving Data from Outside Source using AngularJS

Is there a way to retrieve JSON-Text-Stream data from a specific URL (e.g. SOMEURL/ean.php?id=4001513007704)? The returned result typically appears as follows: { "product": { "ean_id": "4001513007704", "title": "Gerolsteiner Mineralw ...

Attempting to assign a value to the Progress Circle

Can I modify the code to incorporate a hardcoded number instead of displaying "Goals completed:" and still have the progress bar reflect the percentage? I want to hide the prompt for users to input a number and handle all updates behind the scenes. Essent ...

Tips for structuring route dependencies in Node.js and Express

Although I have a good grasp of exporting routes to an index.js file, my struggle lies in properly referencing external route dependencies without having to copy them to the top of the file. For instance, if I have the main entry point of the program (ind ...

Check the dimensions of the image file entered using AngularJS on the client side

Before uploading an image to the server, I need to validate its dimensions on the client side. I have searched for solutions using img.Onload(), but that's not what I am looking for. All I want is for the user to choose the image from <input ...

Whenever I include "border:0" in the styling of my hr element, a significant number of hr elements fail to appear on the screen

I have been experimenting with using hr elements to divide sections of my web page. Here is a snippet of the CSS code I am using: hr{ content: " "; display: block; height: .1px; width: 95%; margin:15px; background-color: #dfdfdf; ...

Can webpack effectively operate in both the frontend and backend environments?

According to the information provided on their website, packaging is defined as: webpack serves as a module bundler with its main purpose being to bundle JavaScript files for usage in a browser. Additionally, it has the ability to transform, bundle, or ...

Using jQuery ajax links may either be effective or ineffective, depending on the scenario. At times

Can someone please assist me in understanding why an ajax link may or may not work when clicked? It seems to function intermittently. window.onload = function(){ function getXmlHttp(){ ...... // simply ajax } var contentContainer = ...

What could be causing the bootstrap columns to automatically shift onto separate lines?

Check out my code snippet: html, body { width: 100%; height: 100%; margin: 0; padding: 0; overflow-x: hidden; } .row { width: 100%; height: 80%; margin: 0px; padding: 0px; } body { background: #EFEFEF; } .container-fluid { wid ...

Concealing tooltip when button is clicked using jQuery

I am facing an issue where the tooltip does not hide on button click. Below is the code for the button in question: <button class="btn btn-outline-inverse ajax_addtocart additems ...

When you hover over the page, the content shifts to reveal a hidden div

Trying to figure out how to show additional content when a photo is hovered over without shifting the rest of the page's content placement? Looking for alternative solutions rather than using margin-top: -22%. Here's a link to the website in que ...

What is the maximum number of files that FileUploader can accept when multi-select is enabled?

I encountered the following issue: A first chance exception of type 'System.Web.HttpException' occurred in System.Web.dll Additional information: Maximum request length exceeded. while attempting to upload 250 JPEG files, each around 98 KB in ...

Create a debounced and chunked asynchronous queue system that utilizes streams

As someone who is new to the concept of reactive programming, I find myself wondering if there exists a more elegant approach for creating a debounced chunked async queue. But what exactly is a debounced chunked async queue? While the name might need some ...

Searching through an array to isolate only image files

I am working with an array that contains various file types, all stored under the property array.contentType. I am trying to filter out just the images using array.contentType.images, I believe. Check out the code snippet below: const renderSlides = a ...

Is your jquery draggable bouncing around unexpectedly on the page when released?

I'm currently troubleshooting the issue of jquery ui draggable moving unexpectedly when released onto the droppable area! The element seems to be shifting to the right regardless of where it is dropped on the page! To better illustrate the problem, ...

What is the most effective method for incorporating parameterized aesthetics using CSS in a JSP program?

I am currently working on a JSP web application without the support of any framework, unfortunately. This application is intended to serve multiple customers, each with their unique set of colors and company logos. The overall layout of the CSS will remai ...

The Cordova imagePickerEx plugin is now also offering a feature to retrieve cached images

When creating a form to upload user data along with multiple images, I am utilizing this plugin. A recurring issue I face is that even after setting "cache view= false", if I select one image, a cached image still appears in the console. <code> var ...

The controller function is not triggered if the user does not have administrator privileges

I have a code snippet in which my controller function is not being triggered for users who do not have the role of "Admin". Can someone help me identify where I went wrong? Just to clarify, the controller function works fine for users with the role of "Adm ...