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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 29, 20262026-05-29T07:55:52+00:00 2026-05-29T07:55:52+00:00

What is the suggested way to solve this issue? I have a HTML list

  • 0

What is the suggested way to solve this issue?

I have a HTML list like so:

<ul>
  <li><img src="img1.jpg" alt="Image One" /></li>
  <li><img src="img2.jpg" alt="Image Two" /></li>
  <li><img src="img3.jpg" alt="Image Three" /></li>
</ul>

and I’d like to convert this data to an object, something like:

{
    images: {
        1: {
            src: "img1.jpg",
            alt: "Image One"
        },
        2: {
            src: "img2.jpg",
            title: "Image Two"
        },
        3: {
            src: "img3.jpg",
            title: "Image Three"
        }
    }
}

Thank you

  • 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-29T07:55:53+00:00Added an answer on May 29, 2026 at 7:55 am

    Here’s one way to do it.

    I explained how it works in the comments.

    Please tell me if there’s anything wrong with the code.

    <!DOCTYPE html>
    <html>
        <head>
            <title></title>
    
            <style>
                </style>
    
        </head>
        <body>
    
    
            <ul id="ul">
                <li><img src="img1.jpg" alt="Image One" /></li>
                <li><img src="img2.jpg" alt="Image Two" /></li>
                <li><img src="img3.jpg" alt="Image Three" /></li>
            </ul>
    
    
            <script>
    
                //Get the element that contains the image nodes
                var ul = document.getElementById("ul");
    
                //create a new 'images' object
                var images = {};
    
    
                //Note: this function call returns the image nodes in the order
                //they appear in the ul element. If an li node has 2 images, or no 
                //images, this method of retrieval may not suit your liking.
                var imgNodes = ul.getElementsByTagName("img");
    
    
                //Loop through all the image nodes
                for(var i = 0, l = imgNodes.length; i < l; i++) {
    
                    //Add a new object to the images object
                    images[i+1+""] = {
    
                        //getAttribute is a safe way to retrieve the attributes of a node
                        src :   imgNodes[i].getAttribute("src"),
                        alt :   imgNodes[i].getAttribute("alt")
    
                    };
    
                }
    
                //Spit it out in the console to make sure it's good.
                console.log(images);
    
    
            </script>
    
        </body>
    </html>
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

In this answer, the suggested way of attaching meta information to types was using
I have a PHP script like this: <?php include 'authorization_script.php'; foreach ($_POST as $key
This is one annoying issue and I can't figure out how to solve it.
Short Version: Is there a/what is the suggested way to return error details to
I have to fix all the closing tags of the <img> tag as shown
I have a .net application using sql server 2005 database. This is a very
Crossposted with STATS.se since this problem could straddle both STATs.se/SO https://stats.stackexchange.com/questions/17712/parallelize-solve-for-ax-b I have some
I have recently stumbled across an issue where the WPF ListView control seems to
I have this code: .h: @interface ColorPickerView : UIViewController { HSBEditView *hsbEditView; } .m:
Edit 4: I was finally able to solve my own issue. See checkmark answer

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.