-4
var csn = ['font-family', 'font-size', 'font-weight', 'color', 'text-decoration', 'text-shadow', 'background-color'];

$.each(csn, function (n, value) {
    style.push("'" + value + "':'" + $('div').css(value) + "'");
});

var msg = "{" + style.join(',') + "}";

$('div#taget').css(msg);
8
  • 4
    What exactly does "not working" mean? Commented Aug 6, 2013 at 13:59
  • where is it not working? Commented Aug 6, 2013 at 13:59
  • 1
    I'm glad you included a snippet of your code, but you need to have a description detailing your problem and exactly what you are trying to accomplish. Also, some HTML might help, as well. Then we might know for sure whether you have a div tag with an ID of "taget" or if it's a typo, which would explain why it is "not working" Commented Aug 6, 2013 at 14:00
  • 2
    I assume taget is a typo... Commented Aug 6, 2013 at 14:00
  • 1
    What makes you assume that .css(value) will return the css setting for a value, while at the same time .css(msg) will set the CSS properties of a div? Commented Aug 6, 2013 at 14:05

4 Answers 4

2

You should pass an object to the css method. '{...}' this is a string. This way you are using the css method as a getter and not a setter, jQuery tries to find and return the value of the requested property(that '{...}' string), which is of course undefined.

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

1 Comment

with all due respect, what did you understand from that question??
1

Corrected :

// use camelCased properties
var props = ['fontFamily', 'fontSize', 'fontWeight', 'color', 'textDecoration', 'textShadow', 'backgroundColor'];

//create a dump object
var dump = {};

//iterate through properties and populate the dump object
for(var i = 0, l = props.length, prop; prop = props[i]; i++)
    dump[prop] = $('div').css(prop);
});

//apply
$('div#taget').css(dump);

You should have read the documentation, where you could have seen some working examples. Also, you look like having a lack of basics in javascript, objects and so on, so i suggest you visit this place.

4 Comments

You should format your "code" as a code-block within the SO editor (Ctrl + K, with the desired code highlighted). This makes code easily distinguishable from the dialogue.
thanks for the keyboard shortcut :)
No prob, for a little added info (I find this very useful): Use Ctrl + Clicking on the actual codeblock button to reduce all highlighted lines by four spaces (the opposite of just clicking the button or using Ctrl + K).
i always used the editor ui, what a shame... thanks again !
0

I didn't tested, but, try this

var csn = ['font-family', 'font-size', 'font-weight', 'color', 'text-decoration', 'text-shadow', 'background-color'];

$.each(csn, function (n, value) {
    $('div#taget').css(value, $('div#source').css(value));
});

look that I change $('div') to $('div#source'), because you want to take the style from just 1 div and not all divs of the page (including div#taget);

Comments

0

@Hey,

have changed some things in your code FIDDLE DEMO

Here is the outcome when you see the source of the div.

<div id="taget" style="font-family:'Times New Roman',font-size:16px,font-weight:400,color:rgb(0, 0, 0),text-decoration:none,text-shadow:none,background-color:rgba(0, 0, 0, 0)">content heer</div>

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.