With jQuery v1.6.4,
I am attempting to dynamically format some objects.
Take a look at my code snippet:
<html>
<head>
<script src="http://code.jquery.com/jquery-1.6.4.min.js" type="text/javascript"></script>
<script type="text/javascript">
$(function(){
style = {'color':'#F30', 'font-size':'20px'};
/* example:1 - not working */
for(attr in style) $('.text').css({attr:style[attr]});
/* example:2 -works */
//$('.text').css({'color':style['color']}).css({'font-size':style['font-size']});
})
</script>
</head>
<body>
<div class="text">This is the text.</div>
</body>
</html>
Why is example 1 not functioning as expected? Is there another method I should consider?
The Chrome console does not display any errors. The "style" object will be formatted as JSON, so I am looking to streamline the code rather than manually specifying each style like in the second example.
Your assistance is greatly appreciated.