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?
CodePudding user response:
Yes, you can dynamically set styles via jQuery.
$(".color-base").css("color", "blue")
CodePudding user response:
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>