3

I want to change a property of external CSS file using javascript/jquery

Example; in the of the web page I'm using:

<!-- Star rating -->
<link href="css/star-rating.min.css" rel="stylesheet" />

And I want to access and change the color of specific style (Found in the external CSS):

CSS

Exists a method using JavaScript or jQuery to do that?

MiBol
  • 1,985
  • 10
  • 37
  • 64
  • Possible duplicate of [How to create a – Paul Roub Nov 18 '15 at 15:47

2 Answers2

1

Ok, let's analyze this....

You want to change a CSS rule on your code. I asume you want to do it as a response to something that happens in the browser...

Change physically the line in the CSS file is not impossible, but I supose that it isn't what you are looking for.

The best option, probably would be to change the CSS style through javascript as a reaction to the event or situation that makes you want to change the style. Using jquery:

$(".rating-container").css("color", "#f0f");

As an alternative, use different CSS classes for different element states and just change classes into your js code:

$("#myAffectedElement").removeClass("oldColorClass");
$("#myAffectedElement").addClass("newColorClass");

This will allow you to modify directly individual elements styles instead of changing every originally classed element.

Bardo
  • 2,470
  • 2
  • 24
  • 42
0

I create a js method to achieve this:

function getStyleSheet(cssName, rule) {
    for (i = 0; i < document.styleSheets.length; i++) {
        if (document.styleSheets[i].href.toString().indexOf(cssName) != -1)
            for (x = 0; x < document.styleSheets[i].rules.length; x++) {
                if (document.styleSheets[i].rules[x].selectorText.toString().indexOf(rule) != -1)
                    return document.styleSheets[i].rules[x];
            }

    }

    return null;
}

To change any property only is required the following:

getStyleSheet('star-rating', '.rating-container .rating-stars').style.color = "#AFAFAF";
MiBol
  • 1,985
  • 10
  • 37
  • 64
  • that's great..exactly i was looking for. Could you please help how can I download or copy the updated css to new file. TIA – Anil Sep 03 '18 at 14:40
  • I have the same question, how can I download or copy the updated css to new file as I am unable to find these changes in sources also. – James Fernandes Mar 22 '19 at 09:16