Forum Moderators: open

Message Too Old, No Replies

Changing Global CSS Styles in JS DOM Manipulation

Tried CSS, to No Avail. Any Ideas Over Here?

         

cmarshall

3:58 pm on Aug 27, 2007 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



NOTE: I had posted this earlier in the CSS forum [webmasterworld.com], but there are crickets chirping, so I think I may have posted in the wrong place (although most of the big voices in CSS also post here, so maybe we can get a cricket duet going).

Here's the deal:

I want to declare a global CSS class, and then use JavaScript to change it after the page loads. It's part of an accessibility/degrading technique (Trick #1) [webmasterworld.com] I use.

Currently, I change the style of individual objects, but it would be a lot more convenient if I could just assign them a CSS class, which, by default, is set to display:none, then change the display property with JS to inherited or block.

The problem is that there does not seem to be a reasonable way to do it. After spending the weekend trawling through various discussions and tutorials, I have come up with dozens of ways to do what I already know, and a few references to changing stylesheets.

However, there does not seem to be a way to change a global style. Let me illustrate:

<html>
<head>
<!-- This is the style in question -->
<style type="text/css">
.javascript_only { display: none }
</style>

</head>
<body>
<!-- This is where the style is applied -->
<div id="js_only_div" class="javascript_only">This will only be displayed if JS is enabled</div>

<div>This will be displayed at all times.</div>
<noscript>This will only be displayed if there is no JS.</noscript>
<script type="text/javascript">
// I want to add code here to change the display property of javascript_only to block, but don't know how.
// This function will change it by ID, but I want to change the class itself, not individual objects.
function ChangeJSDivByID() {document.getElementById('js_only_div').style.display='block'}
</script>
</body>
</html>

I also did some DOM crawling, but that smells like hacking, and probably has a glass jaw (also, nothing I tried worked. CSS DOM seems a bit odd, compared to other stuff on the page).

Any ideas?

[addendum]I know that I can change stylesheets, but that seems to be an overly complex/delicate way to do something so absurdly simple.[/addendum]

Trace

6:46 pm on Aug 27, 2007 (gmt 0)

10+ Year Member



edit: nm, read your question wrong.

[edited by: Trace at 6:47 pm (utc) on Aug. 27, 2007]

Trace

7:05 pm on Aug 27, 2007 (gmt 0)

10+ Year Member



Ok, attempt #2...

<html>
<head>
<!-- This is the style in question -->
<style type="text/css">
.javascript_only { display: none }
</style>
</head>
<body>
<!-- This is where the style is applied -->
<div id="js_only_div" class="javascript_only">This will only be displayed if JS is enabled</div>
<div>This will be displayed at all times.</div>
<noscript>This will only be displayed if there is no JS.</noscript>
<script type="text/javascript">
// I want to add code here to change the display property of javascript_only to block, but don't know how.

if(document.styleSheets){
var sheet = document.styleSheets[0]
if(sheet){
var ssRules = sheet.cssRules ¦¦ sheet.rules;
if(ssRules){
var result = null;
for(var c = 0; c < ssRules.length;c++){
if(ssRules[c].selectorText == ".javascript_only"){
result = ssRules[c].style;
break;
}
}
if(result){
result.display = "block";
}
}
}
}

// This function will change it by ID, but I want to change the class itself, not individual objects.
function ChangeJSDivByID() {document.getElementById('js_only_div').style.display='block'}
</script>
</body>
</html>

Is that what you're looking for?

It takes a little explaining...

The first IF statement checks to see if there is a style sheet then sets the collection to ssRules, in the W3C version the collection is called "cssRules" and on IE it is called "rules", so: var ssRules = sheet.cssRules ¦¦ sheet.rules;

It took a little googling, but it seems to work pretty well.

As always, remember to change the 2 ¦¦ to actual pipes for this to work

cmarshall

8:50 pm on Aug 27, 2007 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Wow. That may be exactly what I'm looking for. Let me try it out and see.

Thanks!

cmarshall

2:40 pm on Aug 28, 2007 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Well, it doesn't work for IE6, but that may be my problem . I'll have to see if I screwed anything up when I translated it.

I found that I can't use styleSheets[0]. I have to use styleSheets[styleSheets.length-1].

Thanks. This is a place to start.

Trace

2:52 pm on Aug 28, 2007 (gmt 0)

10+ Year Member



That is really strange, it works perfectly fine in IE6 here. I'll sticky you the full source I'm using.

cmarshall

3:11 pm on Aug 28, 2007 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



You are correct. Your source works. It probably has to do with the environment. I have it in a pretty complex page with multiple stylesheets. I have to simplify it for posting to WebmasterWorld, but the real world implementation is a bit hairier.

I just need to spend some time figuring it out. I'm sure I'll get it. The ¦¦ thing is a good idea.