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);
4 Answers
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.
1 Comment
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
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
@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>
divtag with an ID of "taget" or if it's a typo, which would explain why it is "not working".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?