/    Sign up×
Community /Pin to ProfileBookmark

Is it possible to change external CSS code via JavaScript?

I know how to change CSS for inline styles, but not quite sure how to do it for things defined in an external stylesheet. Is it possible?

For example, if I have an external stylesheet named myStyles.css with the following:

[code=php]
.myClass {
display: block;
}
[/code]

How can I change it using javascript to be:

[code=php]
.myClass {
display: none;
}
[/code]

to post a comment
JavaScript

3 Comments(s)

Copy linkTweet thisAlerts:
@Vassil_CatsarovFeb 11.2005 — You could change the source of your external css file dynamically to point to a newer css file.
Copy linkTweet thisAlerts:
@wood_tahauthorFeb 11.2005 — Not exactly what I am looking to do. In my real file, I have many other rules defined in my file, so I don't want to have to create a duplicate of my file that has only 1 rule changed.

When the style attribute is defined inline for a tag

[code=php]
<div id="myID" style="display: block;">
content
</div>
[/code]


The code to change it is:

[code=php]
obj = document.getElementById(id);
obj.style.display = "none";
[/code]


What I am looking for is something similar that will let me reference the external stylesheet, the class within that stylesheet, and then the specific rule in the class.
Copy linkTweet thisAlerts:
@FangFeb 11.2005 — [URL=http://www.quirksmode.org/dom/w3c_css.html]Can do[/URL], but there are compatibility problems.
×

Success!

Help @wood_tah spread the word by sharing this article on Twitter...

Tweet This
Sign in
Forgot password?
Sign in with TwitchSign in with GithubCreate Account
about: ({
version: 0.1.9 BETA 5.19,
whats_new: community page,
up_next: more Davinci•003 tasks,
coming_soon: events calendar,
social: @webDeveloperHQ
});

legal: ({
terms: of use,
privacy: policy
});
changelog: (
version: 0.1.9,
notes: added community page

version: 0.1.8,
notes: added Davinci•003

version: 0.1.7,
notes: upvote answers to bounties

version: 0.1.6,
notes: article editor refresh
)...
recent_tips: (
tipper: @AriseFacilitySolutions09,
tipped: article
amount: 1000 SATS,

tipper: @Yussuf4331,
tipped: article
amount: 1000 SATS,

tipper: @darkwebsites540,
tipped: article
amount: 10 SATS,
)...