I just need help how can I get different image for this. I keep getting the same copied image I want the id 1 to have different image. thank you I want to present this as my defense in class.
JavaScript
x
18
18
1
let htmlString = `
2
<div class="col-md-4 card" id="pet_${id}">
3
<img class="card-mg-top pet_img" src="pet3.gif">
4
<div class="card-body">
5
<div>Id: <span class="pet_id">${id}</span></div>
6
<div>Damage: <span class="pet_damage">${data.damage}</span></div>
7
<div>Magic: <span class="pet_magic">${data.magic}</span></div>
8
<div>Endurance: <span class="pet_endurance">${data.endurance}</span></div>
9
Time to Starvation:${deathTime}</span></div>
10
<div class="progress">
11
<div class="progress-bar" style="width: ${percentageString};">
12
13
</div>
14
</div>
15
<button data-pet-id="${id}" class="feed_button btn btn-primary btn-block">Feed</button>
16
</div>
17
</div>`;
18
Advertisement
Answer
It’s because your img src is hard coded to pet3.gif. You probably want to assign a variable name to each image you want with each id.
For eg.
- Image for id 1: pet_1.gif
- Image for id 2: pet_2.gif
- Image for id 3: pet_3.gif…
You get the idea. And then you can have the id inserted in the src the way you’ve done it everywhere else.
JavaScript
1
2
1
src="pet_${id}.gif"
2