This is a common error. i
is a global variable that you're incrementing in the for loop. When the loop finishes, i will be equal to 4. Then in your click handler function, you're trying to display arr[i].innerHTML
, which is now arr[4].innerHTML
. arr[4]
obviously doesn't exist, so you get an error.
For an easy fix, change alert(arr[i].innerHTML)
to alert(this.innerHTML)
. this
, in the context of the click handler function, will refer to the element.
|