3

I would like to get the currentStyle/computedStyle object and I dont want to manually check for the type of browser. (I wanted to get rid of cross-browser handling that is why I came to Jquery). Actually I want the style object returning all the style-property: value map. Please help.

2
  • 1
    Do you need all the styles, or just a select few? The approach would be very different depending on this. Commented Jun 15, 2010 at 12:08
  • see stackoverflow.com/questions/2558426/… and quirksmode.org/dom/getstyles.html, wrap the cross-browser code getStyle function in a jquery plugin or custom selector to isolate it from your code. Commented Jun 15, 2010 at 12:22

1 Answer 1

1

Well, there is a soultion for a similiar problem offered by Keith Bentrup: jQuery CSS plugin that returns computed style of element to pseudo clone that element?

He used a list of attributes from Firebug and created a computed style object with jQuery to be able to clone styles from one object to another:

jQuery.fn.css2 = jQuery.fn.css;
jQuery.fn.css = function() {
    if (arguments.length) return jQuery.fn.css2.apply(this, arguments);
    var attr = ['font-family','font-size','font-weight','font-style','color',
        'text-transform','text-decoration','letter-spacing','word-spacing',
        'line-height','text-align','vertical-align','direction','background-color',
        'background-image','background-repeat','background-position',
        'background-attachment','opacity','width','height','top','right','bottom',
        'left','margin-top','margin-right','margin-bottom','margin-left',
        'padding-top','padding-right','padding-bottom','padding-left',
        'border-top-width','border-right-width','border-bottom-width',
        'border-left-width','border-top-color','border-right-color',
        'border-bottom-color','border-left-color','border-top-style',
        'border-right-style','border-bottom-style','border-left-style','position',
        'display','visibility','z-index','overflow-x','overflow-y','white-space',
        'clip','float','clear','cursor','list-style-image','list-style-position',
        'list-style-type','marker-offset'];
    var len = attr.length, obj = {};
    for (var i = 0; i < len; i++) 
        obj[attr[i]] = jQuery.fn.css2.call(this, attr[i]);
    return obj;
}

This seems to do exactly what you are looking for.

There are also a couple of plugins for that:

  1. http://github.com/peol/jquery-computed-style
  2. http://www.jupiterit.com/news/get-multiple-computed-styles-fast-with-the-curstyles-jquery-plugin
Sign up to request clarification or add additional context in comments.

1 Comment

Even though this is accepted but I feel it would be better if we had something like a cloneStyle function that would copy styles from one element to other.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.