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 6982549
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T18:18:10+00:00 2026-05-27T18:18:10+00:00

I need to loop through a series of DOM elements and create an object

  • 0

I need to loop through a series of DOM elements and create an object literal with jQuery as seen below. I’m guessing this involves the usage of .each, but I’m a bit stuck on what to do next.

    '1': {
        'text': 'Maintain Compliance',
        'desc': 'blah',
        'size': 10,
        'color': '#afb0b3'
    },
    '2': {
        'text': 'lorem ipsum',
        'desc': 'blah.',
        'size': 4,
        'color': '#9b9ca0'
    },
    '3': {
        'text': 'lorem ipsum',
        'desc': 'blah',
        'size': 6,
        'color': '#c5c6c7'
    }
  • 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-27T18:18:11+00:00Added an answer on May 27, 2026 at 6:18 pm

    You’d use .map() to create an Array of the objects.

    var objects = $('.my_elements').map(function(i,el) {
        var $el = $(el);
        return {
            text:$el.text(),
            desc:'blah',
            size:'some_size_property_of_the_element?',
            color:$el.css('color')
        };
    }).get();
    

    The object returned from each iteration is added to the collection.

    This version of .map() actually returns a jQuery object, so you need .get() to convert to an Array.


    You could use the other $.map to create an Array directly.

    var objects = $.map($('.my_elements'), function(el,i) {
        var $el = $(el);
        return {
            text:$el.text(),
            desc:'blah',
            size:'some_size_property_of_the_element?',
            color:$el.css('color')
        };
    });
    

    Notice that the parameters are reversed from the first version. Easy to get caught on that.


    And by the way, you’re not really creating an “object literal”. You’re just creating an object. “Object literal” is a notation used to create objects.


    Also, I’ve assumed by your numeric indices that you want an Array of objects. If the main structure shouldn’t be an Array, then it will need to be a little different, like this…

    var objects = {};
    
    $('.my_elements').each(function(i,el) {
        var $el = $(el);
        objects[ i+1 ] = {
            text:$el.text(),
            desc:'blah',
            size:'some_size_property_of_the_element?',
            color:$el.css('color')
        };
    });
    

    This starts the numbering on 1 as shown in the question. Though I’d still be inclined to use an Array.

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

Sidebar

Related Questions

I need to loop through a selection of elements and need to do things
I need to loop through an array and for each element create a textfield.
I need to loop through some elements in the page and then, for each
I need to loop through all table elements, somewhere keeping an index and then
I need to loop through the properties of a custom object type that I'm
I need to loop through my website using jquery and change the class selector
I have a multi-dimensional array that I need to loop through and create <td></td>
ok so i have this array and i need to loop through <?php foreach($classes
I need to loop through all the matches in say the following string: <a
I need to loop through a set of 2-dimensional array of hidden input fields

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.