0

How to get all css property and its value from an element ?

I am trying to make a plugin for live editing css of an element. If an element have already any style then get it and append value to input fields so that I can edit already set styles.

I dont have the property name, so how to run a loop and and get all css property and its value. and then put it an array.

my dummy code

function get_css(elem){
    var newObj = {};    
    elem.each(function(){
        var css = this.css('?');
        newObj[parts[property]] = parts[value];
    });
    return newObj;           
}

Here is the fiddle http://jsfiddle.net/howtoplease/4Nv2C/

user007
  • 3,203
  • 13
  • 46
  • 77
  • 1
    Take a look at [http://stackoverflow.com/questions/754607/can-jquery-get-all-css-styles-associated-with-an-element][1] [1]: http://stackoverflow.com/questions/754607/can-jquery-get-all-css-styles-associated-with-an-element – c 2 Jul 20 '13 at 10:12
  • Do you want want all CSS rules computed for an element (except maybe inherited ones???) or all the CSS rules you have declared for an element? This is not the same thing. – A. Wolff Jul 20 '13 at 10:20
  • by <> you mean only style you have declared in CSS, right? If ya, this could be more accurate: http://stackoverflow.com/questions/9180184/access-css-file-contents-via-javascript – A. Wolff Jul 20 '13 at 10:27
  • If you could explain why you would need all the css in an array, maybe someone can help you out with an alternative solution, as you're almost certainly going about this the wrong way as this is not something you would normally have to do. – adeneo Jul 20 '13 at 10:29
  • I am making a plugin for live editing css of an element, So I want to get already set style so that I can change them – user007 Jul 20 '13 at 10:34
  • 1
    See this answer http://stackoverflow.com/a/16935800/2522817 I tested it in Chrome and works. Take a look at typo in 6th line mentioned in comment. – mkutyba Jul 20 '13 at 11:55

1 Answers1

0

maybe you could put properties in object

myObj : {

color: 'black'; }

and then maybe you can go over with for in loop and take value or properties and then push into an array

for(var property in myObj){

console.log(property);

}