How to display an image in HTML via a JSON file using Javascript

I have a question about using a JSON file.
I almost have what I want, except the images won’t show on my page. Instead paths to the images are printed out:

The JSON file is like a little database just for the thumbnails of the blog posts, so I don’t have to hard code them. On the print screen, I have already a little bit of what I want. I am not worried about the styling, I will take care of that after I understand this problem.

Here is the JSON file:

[
  {
    "postid": "4",
    "title": "More coming soon",
    "text": "Soon I will add more recipes, this is just the start of a blog full of content! Come back regulary",
    "img": "https://pbs.twimg.com/profile_images/735509975649378305/B81JwLT7.jpg",
    "url": "#"
  },
  {
    "postid": "3",
    "title": "Pizza",
    "text": "The Italian classic, so many different styles of it. Try this recipe at home, for a crusty tastfull pizza!",
    "img": "https://pbs.twimg.com/profile_images/735509975649378305/B81JwLT7.jpg",
    "url": "#"
  },
  {
    "postid": "2",
    "title": "Madeleines",
    "text": "French classic scallop shape little cakes, perfect with a cup of coffee. You can make them easy at home!",
    "img": "../assets/images/recipes/Madeleines.png",
    "url": "#"
  },
  {
    "postid": "1",
    "title": "Moelleux",
    "text": "A special one for the chocolate lovers! The best chocolate dessert is very easy to make, give it a try...",
    "img": "../assets/images/recipes/Moelleux.png",
    "url": "#"
  }
]

Here is the HTML:

  <!--  Jason test  -->
  <div class="search-wrapper">
    <label for="search">Search Users</label>
    <input type="search" id="search" data-search>
  </div>
  <div class="user-cards" data-post-cards-container></div>
  <template data-post-template>
    <div class="card">
      <div class="header" data-header></div>
      <div class="body" data-body></div>
      <div class="photo" data-photo></div>
    </div>
  </template>
  <!--  End Jason test  -->

Here is the Javascript:

const postCardTemplate = document.querySelector("[data-post-template]")
const postCardContainer = document.querySelector("[data-post-cards-container]")
const searchInput = document.querySelector("[data-search]")

let posts = []

searchInput.addEventListener("input", e => {
  const value = e.target.value.toLowerCase()
  posts.forEach(post => {
    const isVisible =
      post.title.toLowerCase().includes(value)
    post.element.classList.toggle("hide", !isVisible)
  })
})

fetch("../jsn/posts-main.json")
  .then(res => res.json())
  .then(data => {
    posts = data.map(post => {
      const card = postCardTemplate.content.cloneNode(true).children[0]
      const header = card.querySelector("[data-header]")
      const body = card.querySelector("[data-body]")
      const img = card.querySelector("[data-photo]")
      header.textContent = post.title
      body.textContent = post.text
      img.textContent = post.img
      postCardContainer.append(card)
      return { title: post.title, element: card }
    })
})

The issue is because you’re just appending the URL to the image as text within the parent div.

To fix the problem you need to create an img element and set the information from the fetch request as the src of that img.

Here’s a simplified example:

// mock AJAX data:
const data = [{postid:"4",title:"More coming soon",text:"Soon I will add more recipes, this is just the start of a blog full of content! Come back regulary",img:"https://pbs.twimg.com/profile_images/735509975649378305/B81JwLT7.jpg",url:"#"},{postid:"3",title:"Pizza",text:"The Italian classic, so many different styles of it. Try this recipe at home, for a crusty tastfull pizza!",img:"https://pbs.twimg.com/profile_images/735509975649378305/B81JwLT7.jpg",url:"#"},{postid:"2",title:"Madeleines",text:"French classic scallop shape little cakes, perfect with a cup of coffee. You can make them easy at home!",img:"../assets/images/recipes/Madeleines.png",url:"#"},{postid:"1",title:"Moelleux",text:"A special one for the chocolate lovers! The best chocolate dessert is very easy to make, give it a try...",img:"../assets/images/recipes/Moelleux.png",url:"#"}];


const postCardTemplate = document.querySelector("[data-post-template]")
const postCardContainer = document.querySelector("[data-post-cards-container]")

// within fetch handler
let posts = []

posts = data.map(post => {
  const card = postCardTemplate.content.cloneNode(true).children[0]
  const header = card.querySelector("[data-header]")
  const body = card.querySelector("[data-body]")
  const img = card.querySelector("[data-photo]")
  
  header.textContent = post.title
  body.textContent = post.text
  
  const imgContent = document.createElement('img');
  imgContent.src = post.img;
  img.appendChild(imgContent);
  
  postCardContainer.append(card)
  return {
    title: post.title,
    element: card
  }
})
<div class="search-wrapper">
  <label for="search">Search Users</label>
  <input type="search" id="search" data-search>
</div>
<div class="user-cards" data-post-cards-container></div>
<template data-post-template>
    <div class="card">
      <div class="header" data-header></div>
      <div class="body" data-body></div>
      <div class="photo" data-photo></div>
    </div>
  </template>

0

Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa Dịch vụ tổ chức sự kiện 5 sao Thông tin về chúng tôi Dịch vụ sinh nhật bé trai Dịch vụ sinh nhật bé gái Sự kiện trọn gói Các tiết mục giải trí Dịch vụ bổ trợ Tiệc cưới sang trọng Dịch vụ khai trương Tư vấn tổ chức sự kiện Hình ảnh sự kiện Cập nhật tin tức Liên hệ ngay Thuê chú hề chuyên nghiệp Tiệc tất niên cho công ty Trang trí tiệc cuối năm Tiệc tất niên độc đáo Sinh nhật bé Hải Đăng Sinh nhật đáng yêu bé Khánh Vân Sinh nhật sang trọng Bích Ngân Tiệc sinh nhật bé Thanh Trang Dịch vụ ông già Noel Xiếc thú vui nhộn Biểu diễn xiếc quay đĩa Dịch vụ tổ chức tiệc uy tín Khám phá dịch vụ của chúng tôi Tiệc sinh nhật cho bé trai Trang trí tiệc cho bé gái Gói sự kiện chuyên nghiệp Chương trình giải trí hấp dẫn Dịch vụ hỗ trợ sự kiện Trang trí tiệc cưới đẹp Khởi đầu thành công với khai trương Chuyên gia tư vấn sự kiện Xem ảnh các sự kiện đẹp Tin mới về sự kiện Kết nối với đội ngũ chuyên gia Chú hề vui nhộn cho tiệc sinh nhật Ý tưởng tiệc cuối năm Tất niên độc đáo Trang trí tiệc hiện đại Tổ chức sinh nhật cho Hải Đăng Sinh nhật độc quyền Khánh Vân Phong cách tiệc Bích Ngân Trang trí tiệc bé Thanh Trang Thuê dịch vụ ông già Noel chuyên nghiệp Xem xiếc khỉ đặc sắc Xiếc quay đĩa thú vị
Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa
Thiết kế website Thiết kế website Thiết kế website Cách kháng tài khoản quảng cáo Mua bán Fanpage Facebook Dịch vụ SEO Tổ chức sinh nhật