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

  • SEARCH
  • Home
  • 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 4249538
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 21, 20262026-05-21T04:24:37+00:00 2026-05-21T04:24:37+00:00

As the title says I am wondering if it is possible to loop through

  • 0

As the title says I am wondering if it is possible to loop through the style attributes of a div with javascript or jquery. What I want to do is loop through the styles and create an object containing these style names and values.

Here is an example of what I am trying to do:

alert($('#mydiv').attr('style'));

Gives the following:

background-color: #CCCCCC; border-width: 2px; border-style: solid; width: 250px;

And I want to create a object which looks like this:

{"background-color":"#CCCCCC","border-width":"2px","border-style":"solid","width":"250px"}

What I can’t figure out is whether this is achievable by looping through the styles or whether I will have to create the object myself using code similar to below:

var style = {};

style['width'] = $('#mydiv').css('width');

Any input of this would be appreciated.

  • 1 1 Answer
  • 3 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-21T04:24:38+00:00Added an answer on May 21, 2026 at 4:24 am

    Version 1, using inline style

    const style = $("#myDiv").attr("style");
    const parts = style.split(";") 
    console.log(parts)
    
    let obj = {}
    parts.forEach(part => {
      if (part.length > 0) { // skip the empty element after the last ;
        const [key,val] = part.split(':');
        obj[key] = val.trim();
      }  
    })
    
    console.log(obj)
    <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.6.0/jquery.min.js"></script>
    <div id="myDiv" style="background-color: #CCCCCC; border-width: 2px; border-style: solid; width: 250px;">My div</div>

    Version 2 using computed style of a list of elements

    // more code needed to handle rgba
    const rgbToHex = rgb => '#' + (rgb.match(/[0-9|.]+/g).map((x, i) => i === 3 ? parseInt(255 * parseFloat(x)).toString(16) : parseInt(x).toString(16)).join('')).padStart(2, '0').toUpperCase();
    
    
    let obj = {}
    const style = window.getComputedStyle(document.getElementById('myDiv')); // 
    ["background-color", "border-width", "border-style", "width"]
    .forEach(rule => {
      const val = style.getPropertyValue(rule)
      obj[rule] = val.includes('rgb') ? rgbToHex(val) : val;
    })
    
    console.log(obj)
    #myDiv {
      background-color: #CCCCCC;
      border-width: 2px;
      border-style: solid;
      width: 250px;
    }
    <div id="myDiv">My div</div>
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

The title says it mostly. If I have an object in Python and want
As the title says, I was wondering if there is any way to create
Much as the title says, I am wondering how to style a program like
Title says it mostly. I want to add a simple extension method to the
Title says all. Sample code: ArrayList<HashMap<String, Object>> data = new ArrayList<HashMap<String, Object>>(); HashMap<String, Object>
As title says, I'm wondering if stacking subviews can slow down an iPhone application.
As the title says I have a list of Django objects and I want
The title says most of it, I'm wondering if MySQL is suitable, (and if
Pretty much like the title says.. I'm wondering where the place is to suggest
The title pretty much says it all. When I'm doing some reflection through my

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.