2

I have a bootstrap Panel and I managed to set the opacity of the body.

<div class="panel panel-default" style="background:none;">
   <div class="panel-heading">What is needed to perform activity?</div>
       <div class="panel-body" id="needsBody">
            Panel content
        </div>
    </div>

using the css as follow

#needsBody {
   background: rgba(255, 255, 255, 0.3) !important;
}

I want to set this the color dynamically using jquery(or javascript) doing it as follow although it doesnt work.

$("#needsBody").css("background", "rgba(175, 201, 99, 0.2) !important;");

How would I go about in doing this effectively?

Arianule
  • 8,811
  • 45
  • 116
  • 174

3 Answers3

7

jQuery ignores the !important declaration as it's simply unnecessary - an element's style attribute (which is what jQuery sets) has higher specificity than CSS can apply (unless your CSS is riddled with !important declarations itself, which is bad practice for this exact reason). Drop the !important and it will work fine:

$("#needsBody").css("background", "rgba(175, 201, 99, 0.2)");

For this to work you'll need to remove the !important from your existing styling:

#needsBody {
  background: rgba(255, 255, 255, 0.3);
}

If that fails to apply the style, simply increase its specificity by improving your selector's strength: div.panel-body#needsBody { ... }, for instance.

James Donnelly
  • 126,410
  • 34
  • 208
  • 218
1

Try using hex instead. It worked for me.

$("#needsBody").css("background", "#AFC963");
Neve12ende12
  • 1,154
  • 4
  • 17
  • 33
1

The CSS property should be background-color and the !important is not needed here. Also, you have a semicolon in your CSS property.

Try with:

$("#needsBody").css("background-color", "rgba(175, 201, 99, 0.2)");

255kb - Mockoon
  • 6,657
  • 2
  • 22
  • 29