Sunday, May 19, 2024
 Popular · Latest · Hot · Upcoming
rated 0 times [  68] [ 3]  / answers: 1 / hits: 43441  / 15 Years ago, tue, june 16, 2009, 12:00:00

I'm looking for a way using jQuery to return an object of computed styles for the 1st matched element. I could then pass this object to another call of jQuery's css method.

For example, with width, I can do the following to make the 2 divs have the same width:


It would be nice if I could make a text input look like an existing span:


where .css() with no argument returns an object that can be passed to .css(obj).

(I can't find a jQuery plugin for this, but it seems like it should exist. If it doesn't exist, I'll turn mine below into a plugin and post it with all the properties that I use.)

Basically, I want to pseudo clone certain elements but use a different tag. For example, I have an li element that I want to hide and put an input element over it that looks the same. When the user types, it looks like they are editing the element inline.

I'm also open to other approaches for this pseudo cloning problem for editing. Any suggestions?

Here's what I currently have. The only problem is just getting all the possible styles. This could be a ridiculously long list.

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',
var len = attr.length, obj = {};
for (var i = 0; i < len; i++)
obj[attr[i]] =, attr[i]);
return obj;

Edit: I've now been using the code above for awhile. It works well and behaves exactly like the original css method with one exception: if 0 args are passed, it returns the computed style object.

As you can see, it immediately calls the original css method if that's the case that applies. Otherwise, it gets the computed styles of all the listed properties (gathered from Firebug's computed style list). Although it's getting a long list of values, it's quite fast. Hope it's useful to others.

More From » jquery


Two years late, but I have the solution you're looking for. Here's a plugin I wrote (by wrapping another guy's function in plugin format) which does exactly what you want, but gets all possible styles in all browsers, even IE.


* getStyleObject Plugin for jQuery JavaScript Library
* From:
* Copyright: Unknown, see source link
* Plugin version by Dakota Schneider (

$.fn.getStyleObject = function(){
var dom = this.get(0);
var style;
var returns = {};
var camelize = function(a,b){
return b.toUpperCase();
style = window.getComputedStyle(dom, null);
for(var i=0;i<style.length;i++){
var prop = style[i];
var camel = prop.replace(/-([a-z])/g, camelize);
var val = style.getPropertyValue(prop);
returns[camel] = val;
return returns;
style = dom.currentStyle;
for(var prop in style){
returns[prop] = style[prop];
return returns;
return this.css();

Basic usage is pretty simple:

var style = $("#original").getStyleObject(); // copy all computed CSS properties
$("#original").clone() // clone the object
.parent() // select it's parent
.appendTo() // append the cloned object to the parent, after the original
// (though this could really be anywhere and ought to be somewhere
// else to show that the styles aren't just inherited again
.css(style); // apply cloned styles

[#99298] Friday, June 12, 2009, 15 Years  [reply] [flag answer]
Only authorized users can answer the question. Please sign in first, or register a free account.

Total Points: 63
Total Questions: 112
Total Answers: 97

Location: Kazakhstan
Member since Mon, Sep 26, 2022
2 Years ago
harrisonnelsonb questions