So when I click on the "hartje" section I am triggering a function. The function is getting all classes with the name "food-option". If I click on the function trigger it is supposed to give the "data-favorite" a value. It does give it a value, but the problem is that I have to click twice so it gets one
const foodOption = document.querySelectorAll(".food-option")
function makeFavo() {
for (var i = 0; i < foodOption.length; i++) {
foodOption[i].addEventListener("click", e => {
if (e.path[2].dataset.favorite == "fav") {
e.path[2].dataset.favorite = ""
e.path[1].children[0].style.backgroundColor = "gray"
e.path[1].children[1].style.backgroundColor = "gray"
e.path[1].style.backgroundColor = "gray"
} else {
e.path[2].dataset.favorite = "fav"
e.path[1].children[0].style.backgroundColor = "red"
e.path[1].children[1].style.backgroundColor = "red"
e.path[1].style.backgroundColor = "red"
}
})
}
}
<section data-favorite="" class="food-option">
<section class="food-picture">
<figure>
<img class="picture" src="./Images/vegetarisch-recept-flatbreads-met-falafel-yoghurt-muntsaus2-1585741275.jpg" alt="">
</figure>
</section>
<section class="food-choice">
<p>falafel</p>
</section>
<section class="favo-food">
<section class="hartje" onclick="makeFavo()">
<section class="hartje2">
</section>
<section class="hartje3">
</section>
</section>
</section>