6

I'll start with a little background.

So what I'm trying to do is grabbing the "style" attribute from an element, and the eventual plan is to output it to a text box (the styling is dynamic). With this I'm creating some css prefixing, due to the fact that I'm only grabing computed styles.

With that, I have a variable with a bunch of css properties as seen here:

compcss = {
                'font-size': fsize,
                'padding': tpadd,

                '-webkit-border-radius': brad,
                '-moz-border-radius': brad,
                '-o-border-radius': brad,
                '-ms-border-radius': brad,
                'border-radius': brad,

                'background': bground,
                'background-m': bgmoz,
                'background-o': bgop,
                'background-i': bgie,
                'color': 'white',
                'text-shadow': '0 -1px 0 rgba(0, 0, 0, 0.25)',
                'text-decoration': 'none',
                'border': '1px solid rgba(0, 0, 0, 0.1) rgba(0, 0, 0, 0.1) rgba(0, 0, 0, 0.25)',

            };

Normally fsize, tpadd, brad, and bground are filled with

document.defaultView.getComputedStyle(cssStr[0], "")[style]

but for the following jsbin I put in static numbers.

This returns as [object Object] when logged or put into the text box, which is to be expected. However, I wish to get this object outputting as a string in the form:

font-size: Xpx;
padding: Ypx;
-webkit-border-radius: Zpx;

and so on, I've tried JSON.stringify(compcss), but that returns as:

"font-fize":"Xpx","padding":"Ypx","-webkit-border-radius":"Zpx"

all the way down the line.

What is the best way to get this to output the way I want? Let me know if anything needs clarifying. Is there a better way of going about this?

here's a jsbin for example: http://jsbin.com/opiwuy/2/edit

Both Vanilla Javascript and JQuery are fine.

Thanks!

5
  • Does it matter if were putting object properties in quotations or not?? Or can we define them with or without? Commented Jun 1, 2012 at 18:06
  • @SethenMaleno Not necessary to have any of the properties in quotations, this is to be used in a stylesheet. I've accepted a solution already, but thank you for your interest. Commented Jun 1, 2012 at 18:10
  • @MoDoFox I am really asking for my own sake. I've never seen properties listed in an object with quotations. Commented Jun 1, 2012 at 18:13
  • @SethenMaleno You know what... I'm not sure why some of them are returning in "'s Commented Jun 1, 2012 at 19:24
  • @MoDoFox You are saying that the properties are returning in ''s? Commented Jun 4, 2012 at 18:31

3 Answers 3

6
  var value = '';
  $.each(compcss, function(key, val) {
    value += key + ' : ' + val + ';' +'\n';
  });
  $('#css').val(value);

DEMO

You can also do

$('#css').val(function() {
    var value = '';
    $.each(compcss, function(key, val) {
      value += key + ' : ' + val + ';' + '\n';
    });
    return value;
});

DEMO

Sign up to request clarification or add additional context in comments.

2 Comments

I was trying to do similar stuff but this breaks the color: "white" as color: white. +1 for the effort.
Thanks! Works great. It seems I have fallen trap to thinking too hard about something that has a fairly simple solution.
2

something like this would probably suit your needs:

function cssStringify(myObject) {
    var result = "";
    var stringObjs = JSON.stringify(myObject).split(",");
    $.each(stringObjs, function(val) {
        var pair = val.split(":");
        result = result + pair[0] + ":" + pair[1] + ";\n";
    });
    return result;
}

Comments

2

Just throwing out my version with .replace

var xx = JSON.stringify(compcss); 
$('#csjson').val(
    xx.replace(/":"|":/g, ":")
      .replace(/,"/g, "\n")
      .replace(/"/g, ""));

DEMO: http://jsbin.com/opiwuy/4

Comments

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.