I'm working on a project with an array of objects representing different pets. Each pet card should display specific information about the pet when clicked, but I'm struggling to make only the relevant information show up. How can I achieve this functionality?
const pets = [
{
"name": "Jennifer",
"img": "../../assets/images/pets-jennifer.png",
"type": "/",
},
{
"name": "Sophia",
"img": "../../assets/images/pets-sophia.png",
"type": "/",
},
{
"name": "Woody",
"img": "../../assets/images/pets-woody.png",
"type": "..",
}
]
document.querySelectorAll('.pet_card').forEach(card => card.addEventListener('click', () => {
modal.classList.add('show-modal')
}))
document.querySelector('.modal').innerHTML = `
${pets.map(petTemplate).join('')}
`;
function petTemplate(pet) {
return `
<div class="modal_wrap">
<div class="modal_img">
<img src="${pet.img}">
</div>
<div class="modal_content">
<h3>${pet.name}</h3>
</div>
</div>
`;
}
<div class="main_pets_wrap">
<div class="pet_card">
<h5>Woody</h5>
<button class="pet_card_btn">Learn more</button>
</div>
<div class="pet_card">
<h5>Jennifer</h5>
<button class="pet_card_btn">Learn more</button>
</div>
.....
</div>