WebReference.com logo
tip archive  •   about  •   sitemap  •   contact  •   jobs  •   write for us  •   subscribe


[prev]

Working with the DOM Stylesheets Collection [con't]

Technical Lead
Thomson Reuters (Markets) LLC
US-NY-New York

Justtechjobs.com Post A Job | Post A Resume

As mentioned earlier, the rule of thumb to follow is to set element properties directly whenever possible. Hence:

is preferable to:

That's fine for one element, but what if we wanted to change an attribute of all the <P> elements in the page, or affect multiple properties? The DOM is powerful enough to handle that as well. Have a look at the following code:

That's still a lot easier than accessing the styleSheet object.

Having said that, there are times where changing the rule makes more sense. For instance, changing element properties by className is more difficult using the DOM because you'd have to traverse the document structure to find them all. And if you change the properties on an ongoing basis, you'd have to store a reference to all the elements. Here's a function that sets the color property of the first rule of the first styleSheet:

This type of function is really useful when you don't know in advance what a property's value will be, such as when it's set by the user, or other runtime factors are involved.

CSS Rule Style Methods

At the rule level, the style object has its own methods for working with individual properties.

To give you an idea of what can be done with these methods, I've written a function that iterates through a styleSheet object and turns off the !important attribute for all properties. The item() function is used to return each property from the style object. The RegExp test(string) function then looks for the !important attribute. At this time, it's the only non-string value that can be returned from the getPropertyPriority() function, so there's no need to be quite as specific as I was here. Notice the use of some special regular expression symbols such as the start and end of string position matchers (^ and $ respectively) and the zero or one occurrence modifier (?). The latter is used because some browsers, like IE for Mac, include the exclamation point, while others don't. If our test evaluates to true, we use the setProperty() function to overwrite the same property, passing in an empty string for the priority:

Most of these functions are part of the DOM level 2 specification, so they're not supported across all browsers, and the ones that do aren't one hundred percent compliant. In fact, my trusty Internet Explorer 6 on my development workstation couldn't process the above function, although no errors were displayed.

The following charts show browser support for each of the properties and methods that we discussed here today.

You may also enjoy:

Now that we've looked at some ways that we can use the DOM styleSheets collection to manipulate rules and their properties, there may come a time that you will recognize a situation where they will come in handy. In the mean time, don't dive into your code and start changing your DOM element handling just yet! Remember our rule of thumb: access document elements directly using the element.style notation whereever possible. If you can't solve a problem that way, then use the DOM styleSheets, but you will have to be thorough in your error handling. Use try/catch blocks whenever possible!

References

Original: November 20, 2008

Social Bookmark


[prev]

Recent Articles

WebReference.com site name
Rolling Out Your Own HTML Application Version Control
HTML 5: Client-side Storage
Working with Ajax Server Extensions
internet.com site name
Wi-Fi Product Watch, November 2009
Chip Market Recovering From '08 Collapse
Low-Cost Tools to Kickstart Your New Business


internet.commediabistro.comJusttechjobs.comGraphics.com

Search:

WebMediaBrands Corporate Info

Legal Notices, Licensing, Permissions, Privacy Policy.
Advertise | Newsletters | Shopping | E-mail Offers | Freelance Jobs