So I've got some code like so:
var word = 'hello'.split('')
var index = 0
for (letter of word) {
button = document.createElement('button')
button.innerText = letter
button.addEventListener('mouseover', function () {
alert(index)
})
index++
document.body.appendChild(button)
}
The problem I'm having is that the alert will always give me the final value of index rather than 0 - 4 for each element.
I'm sure I've seen a solution to a problem like this before, and I'm honestly fully expecting this to get flagged as a duplicate, but for the life of me I can't seem to figure out the proper search terms to find the answer I'm looking for.