-1

I have a css file. Somewhere in it I have this:

.color-base { color: #f0f0f0; }

My question is: is it possible to call .color-base directly on a jQuery script to dynamically set the associated color?

2
  • Hello You can check this thread: Change class attributes dynamically Hope it helps ! Commented May 9, 2022 at 11:09
  • I’m not clear what you want to do. Are you trying to find what value color is set to in that stylesheet .color-base declaration? Commented May 9, 2022 at 12:30

2 Answers 2

0

Yes, you can dynamically set styles via jQuery.

$(".color-base").css("color", "blue")
Sign up to request clarification or add additional context in comments.

Comments

0

If you are asking if you can update the CSS file, the answer is NO. However, you have declared a class in the css file and you need to assign to some element in your HTML and then you can change the color of that element using jQuery, while your CSS file remains unchanged. Like this, css color change part is like rightly pointed out by @Kenny:

<div id="testdiv">This text changes color</div>
<input type="button" value="Change color" id="mybtn" onclick="changecolor()"/>
<script>
  function changecolor(){
    $("#testdiv").css("color","#ff0000");
  }
</script>

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.