I'd like to simply fetch and display data from api, to each item I want to add button where its possible to click and add item to favorite. If im trying to pass item.name as an argument to the function I got an error
index.html:1 Uncaught ReferenceError: Buzz is not defined at HTMLButtonElement.onclick (index.html:1:10)
what is strange for me if I pass item.id it works fine.
async function getAllBeers() {
const response = await fetch("https://api.punkapi.com/v2/beers");
const data = await response.json();
displayAllBeers(data);
}
getAllBeers();
function displayAllBeers(data) {
const beersContainer = document.getElementById("beersContainer");
data.map((beer) => {
console.log(beer.name);
beersContainer.innerHTML += `
<div class='beerContainer'>
<h3 class='name'>${beer.name}</h3>
<img src='${beer.image_url}'></img>
<p>${beer.description}</p>
<button onclick='addToFav(${beer.name})'>Add to Favourite</button>
</div>
`;
});
}
function addToFav(name) {
console.log(name);
}