Unable to alter background color of checkbox with jQuery slide toggle

I've been working on changing the background colour of the toggle button to green when it's turned on. I feel like I've followed all the correct steps, but for some reason, the background just stays grey.

Can anyone point out what might be going wrong?

Here is my fiddle

$('.slider-button').toggle(function(){
    $(this).addClass('on').parent().next('input[type="checkbox"]').attr('checked', 'checked');
    $('.slider-frame').addClass('green');
},function(){
    $(this).removeClass('on').parent().next('input[type="checkbox"]').removeAttr('checked'); 
    $('.slider-frame').removeClass('green');
});

Thank you in advance for any help you can provide.

Answer №1

Implement custom styling

#stage .slider-frame.green {
    background-color: #5fca42 !important;
    -moz-box-shadow: 0 1px 0 rgba(255,255,255,.4), inset 0 1px 3px rgba(0,0,0,.32), inset 0 0 3px rgba(0,0,0,.12);
    -webkit-box-shadow: 0 1px 0 rgba(255,255,255,.4), inset 0 1px 3px rgba(0,0,0,.32), inset 0 0 3px rgba(0,0,0,.12);
    box-shadow: 0 1px 0 rgba(255,255,255,.4), inset 0 1px 3px rgba(0,0,0,.32), inset 0 0 3px rgba(0,0,0,.12);
    background-image: -webkit-linear-gradient(bottom, #5fca42 0%, #5fca42 0.27%, #d8d8d8 100%);
}

See it in action: Fiddle

Answer №2

If you modify the following code snippet, it will work correctly:

#stage .slider-frame.green {
    background: #5fca42 !important;
}

Instead of using background-color:, just use background:

Check out the updated demo here: http://jsfiddle.net/84Qkz/2/

Answer №3

To avoid the current override issue, be sure to include this code in your CSS:

#stage .slider-frame.green {
    background-image:none;
}

Answer №4

This snippet of CSS code has the potential to be your solution

#main .content-area.blue {
    background-color: #3498db !important;
    background-image: -moz-linear-gradient(bottom, #3498db 0%, #3498db 0.27%, #d8d8d8 100%);
    background-image: -o-linear-gradient(bottom, #3498db 0%, #3498db 0.27%, #d8d8d8 100%);
    background-image: -webkit-linear-gradient(bottom, #3498db 0%, #3498db 0.27%, #d8d8d8 100%);
    background-image: linear-gradient(bottom, #3498db 0%, #3498db 0.27%, #d8d8d8 100%);
    -moz-box-shadow: 0 1px 0 rgba(255,255,255,.4), inset 0 1px 3px rgba(0,0,0,.32), inset 0 0 3px rgba(0,0,0,.12);
    -webkit-box-shadow: 0 1px 0 rgba(255,255,255,.4), inset 0 1px 3px rgba(0,0,0,.32), inset 0 0 3px rgba(0,0,0,.12);
    box-shadow: 0 1px 0 rgba(255,255,255,.4), inset 0 1px 3px rgba(0,0,0,.32), inset 0 0 3px rgba(0,0,0,.12);
z-index:9999999;
}

Answer №5

  1. The color of .slider-button is set to white.
  2. .slider-button is nested within .slider-frame.
  3. .slider-frame has an extra CSS rule making it green.
  4. The green color isn't visible because the background of its element is still white.

Solution...

Modify

#stage .slider-frame.green {....}

to...

#stage .slider-frame.green .slider-button {....}

This way, you are changing the color of .slider-button instead of .slider-frame

Check out this demo on JSFiddle 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 ng-bind directive is functional when used with a textarea element, but it does not work with

Recently diving into the world of angularjs, I stumbled upon ng-bind and its interesting functionality when used as a directive for textarea: <input type="text" ng-model="review.start"/> <textarea ng-bind="review.start"> </textarea> I ...

Column flexbox self is not functioning properly with text ellipsis

Is there a way to apply text-overflow: ellipsis to a flexbox element? I know I need to set the width and overflow: hidden, but how can I achieve the ellipsis effect within the grey block when the text overflows? Removing the flexbox property from the < ...

Troubles arise when trying to encapsulate a ReactJS embedded application with Material ui, causing issues with the

Currently, I am integrating a ReactJS application using Material UI styled components within a Wordpress page. To achieve this, I am utilizing webpack for transpilation of the JavaScript. After generating the bundle for the embedded version of the applica ...

Slanted background div that adjusts to the screen size

I'm struggling to create a slanted angle div that remains responsive as the screen size changes. Unfortunately, my current layout breaks completely when the screen gets bigger. If you'd like to take a look at what I've been working on so fa ...

How can I temporarily change an image when clicking on it using JavaScript?

I am working on an image section where I want to change the image for a few seconds when clicked and then revert back to the original image. Here is the code I have tried: <img src="contacticons.jpg" usemap="#image-map" id="imgName"> <a onclick ...

Updating a slider based on the values of two other sliders can be achieved by implementing a

I am working on a project that involves three input sliders. I need the third slider to display the product of the values from the first two sliders. Additionally, I want the value of the third slider to update dynamically whenever the values of the first ...

The content at “http://localhost:3000/script.js” could not be accessed because of a MIME type mismatch with the X-Content-Type-Options set to nosniff

I encountered an issue while attempting to transfer all the JavaScript to a separate js file and then adding that js file to the html per usual. The console displayed the following error message: The resource from “http://localhost:3000/public/main.js” ...

Is selectpicker acting up?

Currently, I am troubleshooting a filter feature on a website that utilizes jQuery with JSON data. Everything was functioning properly until recently when an error started appearing: The selectpicker function is not recognized I would greatly appreciat ...

Having trouble clicking the button due to the overlay blocking it?

This code contains the HTML portion <li id="nav1" class="navs"><a unselectable="on" draggable="false" class="Navigation" href="http://youtube.com">YouTube</a></li> Below is the CSS code .navs:after { content: ""; position: ab ...

Changing CSS module variables through JavaScript programming

I'm currently utilizing an older version of react-boilerplate that includes CSS Modules, allowing for the creation of variables and the ability to import them into other CSS files. Below is my colors.css file: :root { /* Status colors */ --error ...

showing the chosen item in a Bootstrap dropdown [limited to the first menu option and using the attr() method]

Is there a way to dynamically display the selected menu option using the bootstrap drop down? Initially, I was able to select the first menu option and apply it to the bootstrap drop-down button. However, I encountered an issue where I cannot change the ...

The command `browser.find_elements_by_class_name('foo')` returned zero results

I'm encountering an issue with selenium while using Python 3.7. The website I'm working on has a structure similar to this: <html> <body> <div class="foo"> <div class="bar1">some ...

What is the best way to retrieve a modified HTML input field in CodeIgniter PHP?

I am using a CodeIgniter PHP website with a form for users to add and edit products. The editing process is functioning correctly, here is the code for editing a product: View: <form action="" method="post" enctype="multipart/form-data"> < ...

Converting Umbraco Json to a CSS class named "[]"

I'm currently using Umbraco with the data-type grid layout and I am trying to add custom settings (CSS classes) to each row/cell. The results are somewhat working, but I want to improve it. Here's the user interface: https://i.stack.imgur.com/iY ...

How can I retrieve the attribute of the parent or highest level < li > when I am deeply nested within a nested unordered list in jQuery?

I'm trying to navigate up the tree menu of a nested unordered list to the topmost <li> and retrieve the "p_node" attribute using jQuery's toggle function. For example, when I click on "Annie", I want to get the root <li>, which is "mC ...

I am struggling to get the pop-up to close using the current code. I suspect that the issue might be related to the variable I was previously using in WordPress. I have made changes but the pop-up

While delving deeper into the realm of Javascript, I encountered a stumbling block with a single popup intended for the main page of a WordPress website I am constructing. Despite my attempts to modify the code's variables, they seem unyielding. Surpr ...

Using Django to dynamically fill out a form with data retrieved through ajax

My registration form is pretty straightforward, but I need some help with filtering data. There are three additional fields in the form: University Course Module The goal is to allow the user to select a University, and based on that selection, populate ...

How to center an item in a Bootstrap navbar without affecting the alignment of other right-aligned items

Currently, I am in the process of designing a website and have implemented a Bootstrap navbar. The navbar consists of three icons aligned to the right, and I am looking to center another element. However, when I use mx-auto, the element does not center per ...

Discover the method for accessing a CSS Variable declared within the `:root` selector from within a nested React functional component

Motivation My main aim is to establish CSS as the primary source for color definitions. I am hesitant to duplicate these values into JavaScript variables as it would require updating code in two separate locations if any changes were made to a specific co ...

"Error occurs when attempting to insert indexes into a JSON array, causing the Dat

I am facing an issue with my datatable setup. I have a php array that is converted to a JSON array and populates the table successfully. However, when I try to add calculated fields to the table using PHP, it seems like Datatables is unable to read them pr ...