Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6727381
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T10:02:34+00:00 2026-05-26T10:02:34+00:00

Using jQuery v1.6.4, I’m trying dynamically format some objects. Here is my code: <html>

  • 0

Using jQuery v1.6.4,

I’m trying dynamically format some objects.

Here is my code:

<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 example 1 is not working? Are there some other approach?

Chrome console does not shows any error.
Object “style” will be form JSON, so I’m trying to optimize the code, rather to manually write for every style, like in second example.

Thank you for any help.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-26T10:02:35+00:00Added an answer on May 26, 2026 at 10:02 am
    $('.text').css(style);
    

    The reason it is not working is that you have created a javascript literal object. This is like the json you are using to pass parameters in the second example. It is not going to work as it will not loop over your object and so makes it look malformed. You have already done the hard work, so just pass the styles.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Using jQuery what provides for faster object construction: With the HTML code $('<dd class=foo
Using jQuery, I'm trying to group similar items in a list. Here's what I'm
Using jQuery 1.5.1 Tried this block of code in: head section as well as
Using jQuery , how can I dynamically set the size attribute of a select
using jQuery i am trying to find out all the URLS that user has
Using jQuery's UI Tabs. This is my code. <div id=tabs> <ul> <li><a href=#tabs-1>Find a
Using jquery (javascript) need to display facebook user name in html page using facebook
using jQuery, I'm trying to get values of fileTypes by using an index. I'm
Using jQuery how would I find/extract the Spartan string if the outputted HTML page
Using jquery, I am trying to get a background image to fade into a

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.