1

I have a list of dictionaries containing data, where each dictionary has a key 'picture' and a value as an image source (http). 'picture': 'http://....'as well as other key:value sets.

I need to return a html page with a list of names and images associated with the dictionaries.

I have managed to return a list of names but when I try to return the images it returns the image source as a string.

function add_people(data) {
    var element = document.getElementById('people');

    var list = "<ul>";
        for (var i=0; i<data.length; i++) {
            list += "<li>"
                + data[i].name
                + data[i].picture
                + "</li>"
        }
    list += "</ul>";

    element.innerHTML = list
}

This function returns a list of names with the source for the images but not the images themselves, e.g.

  • Captain Pertsy://robohash.org/8c4b1b5a-bb8f-489b-8102-a1e9634627e0

Help is appreciated, thanks.

1
  • 2
    That's what <img> tags are for Commented Apr 8, 2019 at 5:14

5 Answers 5

2

you will need to add image tag inside list element to get the images

 function add_people(data) {
    var element = document.getElementById('people');

var list = "<ul>";
    for (var i=0; i<data.length; i++) {
        list += "<li>"
            + data[i].name
            + "<img src='"+data[i].picture+"' />"
            + "</li>"
    }
list += "</ul>";

element.innerHTML = list
 }
Sign up to request clarification or add additional context in comments.

1 Comment

Thank you so much, I completely understand that now.
0

Use <img> tag to display your images from the source file. In <img> tag, set the source of the image in src attribute.

<img src="http://..." >

Comments

0

Use img tag

<img src=`${url}`>

Comments

0

Use the <img> element if you want to show an image. Use the src attribute to point the element to the source. Here is the modified source.

function add_people(data) {
    var element = document.getElementById('people');

    var list = "<ul>";
        for (var i=0; i<data.length; i++) {
            list += "<li>"
                + data[i].name                  
                + "<img src=\"" + data[i].picture + "\"></img>"
                + "</li>"

        }
    list += "</ul>";

    element.innerHTML = list
}

Additionally, you can use ES6 template strings to easily concatenate strings to build DOM.

list += `<li> ${data[i].name} <img src="${data[i].picture}"></img></li>`

Comments

0

You have to use the img tag and add the picture data as a src attribute. You can also enhance the readability of the code using literal string.

function add_people(data) {
    var element = document.getElementById('people');

    var list = "<ul>";
        for (var i=0; i<data.length; i++) {
            list += `<li>
                      ${data[i].name}
                      <img src="${data[i].picture}" />
                    </li>`
        }
    list += "</ul>";

    element.innerHTML = list
}


Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.