Get image from JSON file using JavaScript and display in HTML img tag

natlotzy

I'm really unsure what I'm doing wrong here. My code makes sense to me, but then again I guess I'm just a beginner. Seems so simple yet I can't figure it out. Any help would be great, please and thank you.

Please read code comments for specifications of what I'm trying to do.

JSON code:

{"images":[
{
    "bannerImg1":"./images/effy.jpg"
}]}

JavaScript:

$.getJSON('data.json', function(data) { // Get data from JSON file
for (var i in data.images) {
    var output+=data.images[i].bannerImg1; // Place image in variable output
}
document.getElementById("banner-img").innerHTML=output;}); // Display image in the img tag placeholder

HTML:

<div class="banner-section">
    <!-- Image should load within the following img tag -->
    <img class="banner-img" alt="effy">
</div>
Sigismundus

Create an Image object (with needed attributes) and add it to the exiting div

var data = {
  "images": [{
    "bannerImg1": "http://i.stack.imgur.com/HXS1E.png?s=32&g=1"
  },
  {"bannerImg1" : "http://i.stack.imgur.com/8ywqe.png?s=32&g=1"
  }]
};
data.images.forEach( function(obj) {
  var img = new Image();
  img.src = obj.bannerImg1;
  img.setAttribute("class", "banner-img");
  img.setAttribute("alt", "effy");
  document.getElementById("img-container").appendChild(img);
});
<div class="banner-section" id="img-container">
    </div>

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

Get image from JSON file using JavaScript and display in HTML img tag

From Dev

Retrieve image from JSON file which is in the database and display in HTML img tag

From Dev

Get image file name from HTML img tag by using RegEx in Notepad++

From Dev

Is there any way to get image data from normal <img> tag using javascript?

From Dev

Display image grid using img tag

From Dev

Load images using html <img> tag from Android expansion file

From Dev

How to get the img elements from within a div tag using JavaScript?

From Dev

What is the difference between using img tag src attribute in HTML to display image and styling it in CSS?

From Dev

html tag <img> and <image>

From Dev

Trouble Using JavaScript to Insert HTML Img Tag

From Dev

display a image that starts with a # in <img> tag

From Dev

how to Get domain name of image tag from html content by javascript

From Dev

How to display an image in android ImageView from the <img> tag

From Dev

Umbraco - How to display an image from a media picker in the src of an IMG tag?

From Dev

Get image tag from html and string content using JQuery

From Dev

javascript select image from img tag without reloading image

From Dev

display image file from json

From Dev

How to display <img> tag using javascript as the active text

From Dev

How to assign JSON image value to HTML img tag

From Dev

HTML img tag not showing the image

From Dev

get the type from input tag in HTML using JavaScript

From Dev

img src to azure blob storage image not working from html tag

From Dev

Passing an image from an img tag to the database or server folder(not choose file)

From Dev

display json into html using javascript

From Dev

XDK - How to load an image from an URL in android using img tag

From Dev

Get the URL of an image from HTML input tag

From Dev

Remove the first img tag in a string of HTML using javascript and regex

From Dev

Remove <img> tag containing a word from Html string using PHP

From Dev

Get the file name from a image source using javascript

Related Related

  1. 1

    Get image from JSON file using JavaScript and display in HTML img tag

  2. 2

    Retrieve image from JSON file which is in the database and display in HTML img tag

  3. 3

    Get image file name from HTML img tag by using RegEx in Notepad++

  4. 4

    Is there any way to get image data from normal <img> tag using javascript?

  5. 5

    Display image grid using img tag

  6. 6

    Load images using html <img> tag from Android expansion file

  7. 7

    How to get the img elements from within a div tag using JavaScript?

  8. 8

    What is the difference between using img tag src attribute in HTML to display image and styling it in CSS?

  9. 9

    html tag <img> and <image>

  10. 10

    Trouble Using JavaScript to Insert HTML Img Tag

  11. 11

    display a image that starts with a # in <img> tag

  12. 12

    how to Get domain name of image tag from html content by javascript

  13. 13

    How to display an image in android ImageView from the <img> tag

  14. 14

    Umbraco - How to display an image from a media picker in the src of an IMG tag?

  15. 15

    Get image tag from html and string content using JQuery

  16. 16

    javascript select image from img tag without reloading image

  17. 17

    display image file from json

  18. 18

    How to display <img> tag using javascript as the active text

  19. 19

    How to assign JSON image value to HTML img tag

  20. 20

    HTML img tag not showing the image

  21. 21

    get the type from input tag in HTML using JavaScript

  22. 22

    img src to azure blob storage image not working from html tag

  23. 23

    Passing an image from an img tag to the database or server folder(not choose file)

  24. 24

    display json into html using javascript

  25. 25

    XDK - How to load an image from an URL in android using img tag

  26. 26

    Get the URL of an image from HTML input tag

  27. 27

    Remove the first img tag in a string of HTML using javascript and regex

  28. 28

    Remove <img> tag containing a word from Html string using PHP

  29. 29

    Get the file name from a image source using javascript

HotTag

Archive