HTML5 Zone is brought to you in partnership with:

Computer Science as profession and hobby, has about ten years of Java experience. Firm believer of software engineering and lover of agile methodologies. Always interest on GIS related technologies, and conceretely, in data visualization since I began working with weather radar data. Skills in Java, HTML, JavaScript, CSS, Databases PHP, and.. always positive. Antonio is a DZone MVB and is not an employee of DZone and has posted 24 posts at DZone. You can read more from them at their website. View Full User Profile

Look Mom No jQuery !!! Getting All CSS Properties of a DOM Element in Pure JavaScript

07.11.2012
| 3029 views |
  • submit to reddit

Getting and setting CSS properties is a simple task. We can make it in pure JavaScript:

var color = domElem.style.color;    // Get color
domElem.style.color='blue';    // Set new color

Using jQuery it can be done with the $.css() method:

var color = $(domElem).css('color');    // Get color
$(domElem).css('color', 'blue');    // Set new color

The recent jQuery++ project claims to have a $.styles() method fastest than $.css() one.

The issue is: How to get all the styles of a given DOM element? Not only those applied directly with the style attribute but those applied using CSS classes.

The solution

Next code retrieves an object with all the style properties of a given DOM element, no matter if they were specified in CSS classes or withint the style attribute of the element.

The idea behind the code is simply: we invoke the browser function that computes the whole element’s style or, if it is not possible, retrieves the CSS properties from the style attribute.

 

function getComputedStyle( dom ) {
        var style;
        var returns = {};
        // FireFox and Chrome way
        if(window.getComputedStyle){
            style = window.getComputedStyle(dom, null);
            for(var i = 0, l = style.length; i < l; i++){
                var prop = style[i];
                var val = style.getPropertyValue(prop);
                returns[prop] = val;
            }
            return returns;
        }
        // IE and Opera way
        if(dom.currentStyle){
            style = dom.currentStyle;
            for(var prop in style){
                returns[prop] = style[prop];
            }
            return returns;
        }
        // Style from style attribute
        if(style = dom.style){
            for(var prop in style){
                if(typeof style[prop] != 'function'){
                    returns[prop] = style[prop];
                }
            }
            return returns;
        }
        return returns;
    };

 The code works (or must work) on any browser. For FireFox and Chrome browser the right way to get the computed style is through the window.getComputedStyle() method, while on IE and Opera browsers the dom.currentStyle property is the right place.

Published at DZone with permission of Antonio Santiago, author and DZone MVB. (source)

(Note: Opinions expressed in this article and its replies are the opinions of their respective authors and not those of DZone, Inc.)

Comments

Kookee Gacho replied on Wed, 2012/07/11 - 8:18am

This enables developers to create abstractions for low-level interaction and animation, advanced effects and high-level, theme-able widgets. The modular approach to the jQuery library allows the creation of powerful dynamic web pages and web applications.-Arthur van der Vant

Comment viewing options

Select your preferred way to display the comments and click "Save settings" to activate your changes.