I want to add innerHTML to my class without changing the text when I hover over it.
For example I have a list of <p>
elements with class="food", and when I hover it I want it to go from saying "Chicken" to "Chicken [EAT]".
This is what I have so far, but when I hover the paragraphs, it changes to the last food item, as expected.
var food = document.getElementsByClassName("food");
var len = food.length;
for (i = 0; i < len; i++) {
addEvent(food[i]);
var foodName = food[i].innerHTML;
function addEvent(food) {
food.addEventListener("mouseover", function(event) {
event.target.innerHTML = foodName + " [EAT]";
});
}
}
<p class="food">Snackbar</p>
<p class="food">Apple</p>
<p class="food">Pear</p>
<p class="food">Chicken</p>
<p class="food">Peas</p>