• Quick note - the problem with Youtube videos not embedding on the forum appears to have been fixed, thanks to ZiprHead. If you do still see problems let me know.

Scripting the DOM by class in html

Rat

Not bored. Never bored.,
Joined
May 19, 2003
Messages
10,629
Location
Leicester, UK
I should know this, but it's driving me up the wall. I have several div enclosed blocks of text. Some of them have a class of win2k, and some have a class of winxp.

I want a script which will detect the OS and display only those relevant to the OS. This will be in an environment where only win2k and winxp are present.

Obviously the first step is to quiz the navigator.appVersion object, and I think I've about got that with
if (navigator.appVersion.indexOf("NT 5.0") > -1)
for Win2k, and similar for XP.

My question, then. How do I alter the display property of several objects based upon their class? I can do it by id, but I don't know how to address the class. I assume that it's document.all.somethingorother. But I can't think what.

Any ideas.

Cheers very much,
Rat.
 
You'll need to figure out the number of your stylesheet and the number of the class entry as seen by JavaScript. It's normally in the same order as it appears in the file, starting with 0. So, if it's all in one style sheet, and you've got one class defined above win2k and winxp then your code would look something like this:

Code:
document.stylesheets[0].cssRules[1].style.display = 'none';
document.stylesheets[0].cssRules[2].style.display = 'block';

Or vice-versa as needed. Check for browser compatibility on this, though. Some of them can be funky. But this code should work with most of them.
 

Back
Top Bottom