Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

114
Vistas
Display images on the browser with map method using url's coming from an API

I'm trying to display two images on the browser based on url's I got from an API. So,

  1. I have fetched the data
  2. run a map method that will run all over the array that I got, and whenever it running into an image it inserts it into a empty div using innerHTML.

and this is what i got:
enter image description here

HTML:

   <body>
        <button id="new-deck">New Deck, Please!</button>
        <button id="draw-cards">Draw</button>
        <script src="index.js"></script>
        <div id="container-card"></div>
    </body>

JS:

    let deckId
let container = document.getElementById('container-card')
function handleClick() {
    fetch("https://deckofcardsapi.com/api/deck/new/shuffle/?deck_count=1")
        .then(res => res.json())
        .then(data => {
            console.log(data)
            deckId = data.deck_id
        })
}

document.getElementById("new-deck").addEventListener("click", handleClick)

document.getElementById("draw-cards").addEventListener("click", () => {
    fetch(`https://deckofcardsapi.com/api/deck/${deckId}/draw/?count=2`)
        .then(res => res.json())
        .then(data => data.cards.map(item =>container.innerHTML += `<img src=${item.image}/>`))      
})
about 4 years ago · Juan Pablo Isaza
1 Respuestas
Responde la pregunta

0

There are two issues:

  1. you are not submitting deckId during image fetch api call.
  2. image src url must be enclosed into quotes, otherwise backslash is added to the end of the url:

    let deckId
let container = document.getElementById('container-card')
function handleClick() {
    fetch("https://deckofcardsapi.com/api/deck/new/shuffle/?deck_count=1")
        .then(res => res.json())
        .then(data => {
            console.log(data)
            deckId = data.deck_id
        })
}

document.getElementById("new-deck").addEventListener("click", handleClick)

document.getElementById("draw-cards").addEventListener("click", () => {
    fetch(`https://deckofcardsapi.com/api/deck/${deckId}/draw/?count=2`)
        .then(res => res.json())
        .then(data => data.cards.map(item =>container.innerHTML += `<img src="${item.image}"/>`))      
})
        <button id="new-deck">New Deck, Please!</button>
        <button id="draw-cards">Draw</button>
        <script src="index.js"></script>
        <div id="container-card"></div>

Every time you are dealing with API calls, make sure you check what data you receive before you minimalistic your code with one-liners. and use catch for promises to catch any errors that might occur.

about 4 years ago · Juan Pablo Isaza Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda