Changing CSS styles with Javascript

M

matturn

Is it possible to change one of the CSS style attributes of say "ul
li"? Not a class or id, but a subset of HTML elements. If so, how
would this be done?
 
D

David Mark

Is it possible to change one of the CSS style attributes of say "ul
li"? Not a class or id, but a subset of HTML elements. If so, how
would this be done?

Use getElementsByTagName to get the unordered lists, then use the same
on each list to get the items. Loop through the items returned from
each list and change the style properties.

There are libraries out there that shield you from writing these sorts
of loops and I am sure you will hear about them shortly.
 
M

matturn

Use getElementsByTagName to get the unordered lists, then use the same
on each list to get the items. Loop through the items returned from
each list and change the style properties.

There are libraries out there that shield you from writing these sorts
of loops and I am sure you will hear about them shortly.

Thanks. I've found some code to do this I can modify.
 
D

David Dorward

Is it possible to change one of the CSS style attributes of say "ul
li"? Not a class or id, but a subset of HTML elements. If so, how
would this be done?

Actually changing their attributes? Only by looping over them.

Its faster and easier to have a prepared ruleset in your stylesheet
that targets ul.foo li (or similar) and then to change the className
of the ul element.
 
D

David Dorward

You can even dynamically insert CSS rules with better luck than
iterating every element.

And you are responding with that to my suggestion why? Please aim your
quoting better.

Last time I looked at dynamically changing the stylesheet itself it
wasn't feasible as, between the major browsers, one had a proprietary
method for doing it, one or two supported the standard, and another
didn't provide any method to do it at all.

Has that changed?
 
D

David Mark

And you are responding with that to my suggestion why? Please aim your
quoting better.

Last time I looked at dynamically changing the stylesheet itself it
wasn't feasible as, between the major browsers, one had a proprietary
method for doing it, one or two supported the standard, and another
didn't provide any method to do it at all.

Has that changed?

Last I checked, it can be done across the latest versions of major
browsers, but it is messy. IE6 is the culprit. If you want any
semblance of backward compatibility with older browsers and hope to
have your app run on mobile devices, appliances, etc. you shouldn't do
it.
 
R

RobG

matturn said:
Is it possible to change one of the CSS style attributes of say "ul
li"? Not a class or id, but a subset of HTML elements. If so, how
would this be done?

Yes, here is code by Martin Honnen that shows how to add and modify CSS
rules:

<URL: http://www.thescripts.com/forum/post343885-2.html >


I posted this link a couple of days ago, it is always good to search the
archives before asking a specific question. They are an amazing resource.
 
D

David Dorward

Looks like it doesn't work (or at least, hasn't been tested) in Opera
or Safari/Konq.

Whoops. Opera is mentioned, but it uses an entirely different
technique that doesn't appear to allow existing rules to be modified.

Hmm, and the example uses invalid CSS.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,580
Members
45,054
Latest member
TrimKetoBoost

Latest Threads

Top