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?

1

2 Answers 2

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.

Sign up to request clarification or add additional context in comments.

Comments

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";

2 Comments

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
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.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.