Updating the appearance of selected radio buttons with CSS

Here is the code I am using to create a toggle switch instead of radio buttons, and it works great:

.switch
{
    display: block;
    float: left;
    -moz-border-radius: 6px;
    -webkit-border-radius: 6px;
    border-radius: 6px;
    font-weight: bold;
    text-transform: uppercase;
    background: #eee;
    border: 1px solid #aaa;
    padding: 2px;
    box-shadow: inset 0 2px 5px rgba(0,0,0,0.2);
    margin-left: 20px;
}

.switch input[type=radio]
{
    display: none;
}

.switch label
{
    display: block;
    float: left;
    padding: 3px 6px;
    -moz-border-radius: 5px;
    -webkit-border-radius: 5px;
    border-radius: 5px;
    color: #aaa;
    cursor: pointer;
}

.switch label:hover
{
    text-shadow: 0 0 2px #fff;
    color: #888;
}

.switch label.checked 
{
    background: #8fc800;
    color: #eee;
    text-shadow: 0 -1px 1px rgba(0,0,0,0.5);
    background: -webkit-linear-gradient(top, #8fc800, #438c00);
    background: -moz-linear-gradient(top, #8fc800, #438c00);
    background: -ms-linear-gradient(top, #8fc800, #438c00);
    cursor: default;
}

Here is the HTML code for the switch:

<div class="switch">
    <input type="radio" name="weekly_new" id="weekSW-0" <?=$yes_checked?> value="Yes" />
    <label for="weekSW-0" id="yes">ON</label>
    <input type="radio" name="weekly_new" id="weekSW-1" <?=$no_checked?> value="No" />
    <label for="weekSW-1" id="no">OFF</label>
</div>

And here is the jQuery code:

<script>
     $(".switch label:not(.checked)").click(function(){
        var label = $(this);
        var input = $('#' + label.attr('for'));

        if (!input.prop('checked')){
            label.closest('.switch').find("label").removeClass('checked');                        
            label.addClass('checked');
            input.prop('checked', true);
        }
    });

    $(".switch input[checked=checked]").each(function(){
        $("label[for=" + $(this).attr('id') + "]").addClass('checked');
    });
    </script>

I want to have a different color for when No is selected as opposed to yes - red for no, green for yes. Can this be done with the current code?

I tried to assign IDs to the labels and style them accordingly:

.switch label.checked #yes { blah blah }

But this rendered the style useless. Any suggestions on how to achieve this with the existing code?

I've created a fiddle for easier editing:http://jsfiddle.net/aS69U/

Answer №1

Check out this CSS snippet for your code:

.toggle #yes.checked {
    background: blue;
}

See the demo here


Nice touch with the customization!

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

Using jQuery to create an array variable and Passing the array to a different PHP page

Although I have managed to create a function that works perfectly for me, I am facing an issue with the jQuery part at the bottom. Currently, I am only able to trigger an alert message and not store the data as a real variable. The function is functionin ...

Eliminate any undefined data in the popup map of Javascript

I am working with JSON data that is being displayed in a pop-up on a map. In cases where there is missing data (Visibility), the word undefined appears in the pop-up. Is there a way to remove the undefined text so that it does not show up in the pop-up? ...

Transforming a jQuery snippet to display an additional element

I am currently implementing a code that displays a "follow me" box on the left side of my website. My goal is to add a second button right below this one, which will redirect users who click it to an RSS link. Any suggestions on how I can achieve this? ...

Using input masking to restrict user input to only numbers and English alphabet characters

My input field is masked as "999999999" and functions correctly with an English keyboard. However, I am able to enter Japanese/Chinese characters into it, which breaks the masking. Is there a way to limit input to English keyboard numerics only? <inpu ...

Difficulty encountering issues with JQuery's ajax function

I've encountered different errors in FF, Chrome, and IE, but it seems like there's an issue with the data in $.ajax. Below is the code snippet. Please be gentle with feedback if I have overlooked something obvious. I've spent countless hours ...

Line breaking by syllables using CSS

When it comes to CSS properties, the word-break attribute is able to split words across lines at specific points (such as the first character extending beyond a certain length). Surprisingly, there seems to be no existing CSS method (even with limited supp ...

The imported font used in Firefox is displaying with a striking underline text-decoration

I am currently using the Cardiff font in a project and attempting to apply the style text-decoration:underline to it. While this works perfectly in Chrome (Version 35.0.1916.114), in Firefox (Version. 29.0.1) the underline appears to be crossing through t ...

Tips on how to include a unique style sheet for IE8 in WordPress

Struggling with responsive behavior in IE8, some parts of my Wordpress website at are not displaying correctly due to issues with the @import query. To tackle this problem, I am considering creating a separate style sheet for IE6, 7, and 8 using the follo ...

Using CSS to create a strikethrough effect on prices with spacing

I have a woocommerce setup and I want to showcase the original prices by striking them out like a price tag when products are on sale. However, there seems to be an issue with the space that woocommerce automatically adds between the currency and the pric ...

Add the response from the API to a table

Looking to extract data from a public holidays API and present it on a leaflet map through a table in a pop-up. The pop-up is functional, and the data is visible in the console but not appearing in the actual pop-up. The code does not add new rows to the ...

Tips for managing cookies in an Angular.js project

At the moment, I am utilizing an AngularJS application. To store certain values in a cookie, I incorporated the angular-cookies-min script to add values to cookies. The following code snippet was used to save values to a cookie: $cookieStore.put("userIn ...

How can I retrieve the text from two DIV elements simultaneously using JS/jQuery?

Is there a way to loop through all <TD> elements in order to store the Title and Link from each element into separate variables using JavaScript / jQuery? Sample HTML: <td> <div class="class_Title row border-bottom" name="name_Title" i ...

Dynamically linking tabbable tabs is a useful technique that allows for

I have been working on an app that organizes websites into groups displayed as tabs in a tabbable navigator using Twitter Bootstrap. The idea is that each time a new group is added, a new tab should appear. Currently, this is how it looks: The functionali ...

The <img> element is able to fill an entire div while maintaining its original aspect ratio

I am currently facing a challenge with implementing a responsive gallery slider in Bootstrap. In order to achieve responsiveness, I need to use properties like max-height. The issue arises from the fact that the images in the gallery can vary in size and ...

The navigation menu collapses upon itself when resizing the browser window

Every time I try to adjust the browser size, the navigation menu seems to collapse within itself, creating a strange effect. I can't figure out what's causing this issue. I've experimented with max-width and sometimes including the "wrapper ...

Trouble with highlighting the chosen menu item

I have been attempting to implement this code snippet from JSFiddle onto my website. Although I directly copied the HTML, used the CSS inline, and placed the Javascript in an external file, the functionality does not seem to be working properly. Feel free ...

Difficulty integrating Bootstrap with JavaScript file in Visual Studio Code

I am attempting to incorporate a dynamic progress bar by utilizing Bootstrap's progressbar and creating a custom JavaScript file. The goal is to continuously adjust the width of the progress bar at regular intervals to give the appearance of a moving ...

jQuery AJAX File Upload problem in Internet Explorer

Encountering an issue with Internet Explorer While this script with AJAX and jQuery functions perfectly in other browsers, it fails to do so in IE index.html <form enctype="multipart/form-data" method="post"> <input name="file" type="file" ...

What are the specific files I should modify for HTML/CSS in my Ruby application?

My application was initially created with an introduction from: http://guides.rubyonrails.org/getting_started.html I am now looking to add some color and style through CSS. I have located the JavaScript and CSS files, but I am unsure which file is respons ...

Populating a div with letter-spacing

I'm facing a challenge in populating a div with text using letter-spacing. The issue at hand is that I am unsure of the width of the div. Initially, my thoughts leaned towards using text-align= justify, however, I found myself lost in the maze withou ...