I would like to fill the name and value of buttons using the key value pairs of an object.
This is the object:
let toppings = {
pepperoni: 0.25,
meatballs: 0.35,
mushrooms: 0.35,
olives: 0.2,
};
These are the buttons:
<button class=" add-cart" name="" value="">
<button class=" add-cart" name="" value="">
<button class=" add-cart" name="" value="">
<button class=" add-cart" name="" value="">
Is it possible? How would I do that? So far, I could loop through the buttons but I don´t know how to add the values. I´ve tried with a nested loop but It does not work!
let addCart = document.querySelectorAll('.add-cart');
for (let btn of addCart) {
btn.name = ?
btn.value = ?
}
Object.keys(toppings)andaddCartsimaltaneously with one loop using an index counter and fillbtn.valueastoppings[key]andbtn.innerHTMLas thekey