Is it possible to change the background color of a Bootstrap button using CSS overrides?

Desired Outcome

Actual Result

The goal is to toggle the red-background class, so that when hovering over the button-bullet, its background-color changes to #FCC1C5.

Avoiding the use of

.btn.button-bullet:hover</code due to jQuery logic disabling a button from showing with only one bullet element displayed.</p>

<p>Despite using <code>toggleClass("red-background")
, there are no visible changes happening.

Possibly the issue lies in CSS where .btn.button-bullet's background-color is set to transparent. If this is the problem, how can the background-color be overridden using jQuery?

Code Snippets

HTML

<div class="worksheet-problems">
  <div class="row">
    <div class="col-lg-7">
      <div class="form-group">
        <div class="input-group input-group-lg worksheet-problem">
          <div class="input-group-btn">
            <button type="button" class="btn btn-default button-bullet"> <span class="glyphicon glyphicon-one-fine-dot" aria-hidden="true"></span> </button>
          </div>
          <input type="text" name="Worksheet-Problem" class="form-control" placeholder="Problem..." aria-label="Write worksheet problem here">
          <div class="input-group-btn">
            <button type="button" class="btn btn-default button-add" aria-label="Add"> <span class="glyphicon glyphicon-plus-sign" aria-hidden="true"></span> </button>
          </div>
        </div>
      </div>
    </div>
  </div>
</div>

CSS

.red-background{
  background-color: #FCC1C5;
}
/*remove Bootstrap default grey for buttons*/
.btn.button-add, .btn.button-bullet{
  background-color: transparent;
}

jQuery

$(document).ready(function() {
  $(".worksheet-problems").on("mouseenter mouseleave", ".button-bullet", function() {
    if ($(".worksheet-problems").children().length > 1) {
      $(this).children(".glyphicon").toggleClass("glyphicon-one-fine-dot");
      $(this).toggleClass("red-background");
      $(this).children(".glyphicon").toggleClass("glyphicon-minus-sign");
    }
  });
});

JSFiddle

Answer №1

Issue: The specificity of your red-background class is lower than .btn.button-bullet.

Resolution (increase specificity and position it after selectors with a transparent background):

.btn.button-add, .btn.button-bullet {
  background-color: transparent;
}
.btn.red-background {
  background-color: #FCC1C5;
}

Answer №2

It appears as though

$(this).toggleClass("red-background"); 

is not affecting the background element. To ensure you are targeting the correct element, use:

console.log($(this)); 

to verify that you are altering the intended element. In some cases, a helpful trick is to utilize:

.red-background{
    background-color: #FCC1C5 !important;
}

However, the issue may also stem from a bootstrap style taking precedence over your CSS class. Make your selector more specific.

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

Issues have been encountered with jQuery on function when it comes to dynamically inserted elements

I have been trying to utilize the .on method for dynamically created elements, but it doesn't seem to be working as expected. I am using a selector though :) $(".metadata").on("click", "i.icon-remove", function () { console.log("what"); $(thi ...

Show singular array element upon click

I need help with a specific task. When a button is clicked, I want to display a random item from an array. Each array item should only be displayed once. Currently, my code is set up as follows: When the button is clicked, a random array item is displ ...

adaptive height that scales with content proportionally

I am looking to achieve a responsive height based on the content inside my container. For example, I want the height of the container to adjust according to the text it contains. <!DOCTYPE html> <html lang="en"> <head> < ...

The parseJSON function is not compatible with AJAX requests

I am attempting to use ajax and retrieve JSON data in WordPress. Here is my Ajax code: $.ajax({ type: "POST", dataType : 'html', url: "/wp-content/themes/myproject/ajax/otros_alojamientos.php", da ...

jQuery slider triggering a function with a consistent delay of 1 until the handle is released

I've been trying to figure out this issue for a while now and haven't found a solution yet. If you take a look at my project here, you'll see that the values at the top don't update correctly until the handle is released. I've been ...

Closing the JQuery login popup form by clicking on the submit button

I have successfully created a popup login form using jQuery. However, I am facing an issue where, upon clicking the submit button, the popup closes and displays an error message stating "wrong username and password". Ideally, I would like the popup form ...

Fade out the notification div when the user clicks anywhere outside of it

I am currently working on a website with a notification feature. When the user clicks on the notification button, a notification box will appear at the bottom of the button. I would like the behavior of this notification to be similar to Facebook's - ...

What is the process for inspecting the element within a div using jsname with JS?

Struggling to inspect elements within a DIV without using xpath. // Assert.assertEquals("Digite uma senha",driver.findElement(By.xpath("//*[@id=\"view_container\"]/div/div/div[2]/div/div[1]/div/form/span/section/div/div/div[1]/div[2]/div[2] ...

Click to dynamically toggle classes with jQuery

I am trying to apply a class called 'select' when I click on a paragraph tag. However, the code I have written doesn't seem to be working. Can someone please provide some suggestions? Here is the code: <style type="text/css"> #el ...

When hovering over a div, reveal another div on top of a third div

Imagine two adjacent divs, A on the left and B on the right with some other elements in between. Is it possible to have a third div, C, appear over div A when hovering over div B? I can control the display property using CSS, but I am unsure how to make d ...

Using the jQuery $.each method with $.getJSON and struggling to access data within a nested array

I've been trying to achieve the same functionality in my jQuery code as my PHP code below, but so far I haven't been able to find any helpful information after nearly two days of searching. <script> $(document).ready(function(){ var s ...

Creating a responsive card design with Material UI

Creating a responsive card layout for mobile applications is my current challenge const styles = { edit: { width: "40%", marginLeft: 270, background: "#76ff03" }, add: { width: "100%", background: "#18ffff", size: "large" ...

What is the process for incorporating beforeAnimate and afterAnimate callbacks into a custom jQuery plugin?

Let's imagine I have developed a plugin: // creating the plugin (function($){ $.fn.myPlugIn = function(options){ defaults = { beforeAnimate : function(){}, afterAnimate : function(){} ...

Reposition div when clicked

I have encountered a challenge where I am unable to perform a small task. My goal is to have the position of "div1" change upon clicking on "div2", taking into account that "div2" is nested inside "div1". Additionally, when clicking on "div2" again, "div1" ...

"Sending an array in a POST request using Javascript and processing it on the server

I am trying to use ajax to send an array. Let's say the array looks like this: var anotherOne = 'anotherOneData'; var documents = ['banana', 'apple', 'monkey']; I successfully sent both a normal value and an a ...

Adding multiple files and additional inputs to formData for PHP processing

I am dealing with a situation where I have multiple forms that are generated dynamically. In addition to handling these forms, I also need to construct the formData for multiple files along with some extra hidden inputs. Here is the approach I have taken ...

jQuery Event not Triggering on Newly Added Element

I have been searching for a solution to this problem and despite coming across numerous options (many of which are outdated), nothing I have attempted has proven successful. The issue arises with my file upload script, where dynamically added table rows c ...

Encountering a problem with Material UI where the drop-down options are appearing below the footer of the modal window

Hey there, I'm currently using Material UI and React Select. One issue I've run into is that my dropdown options are appearing below the modal window. You can check out the code sandbox demo here I've tried adjusting the z-index and changi ...

Executing JavaScript code using an HTML form submission

Greetings, I have implemented an HTML form on my website for user login using AJAX (jQuery), but I am encountering some issues. Below is the JavaScript code: function validateLoginDetails() { $('[name=loginUser]').click(function() { ...

Efficiently managing multiple editing forms on a single Rails 4 page

I am working on creating a one-page brochure in Rails 4. I'm looking to edit multiple forms on a single page. How can I accomplish this? I checked out Railscast 346 Wizard form, but it didn't provide the solution I needed. I want something simi ...