First of all I think you didn't want to write this
Code:
if(this.value === this.value)
but rather this
Code:
if(this.value === oldValue)
But nevertheless it won't work.
You stumbled upon the problem of "closures and loop variables". Closures are "aware" of their surrounding scope even if the code of the scope has finished executing. This is correct. But a closure is not aware of the surrounding scope from the time it has been created but rather from the point of time where it's executed! That's a big difference especially when using loop variables.
At the time of execution, the loop has already finished, so the loop variables (and oldValue is kind of a loop variable here) will ONLY represent the value of the final loop for ALL the event listeners created inside the loop.
Solution: You'll need to do something like this (create an inner closure from an immediately called anonymous function)
Example:
Code:
var myLoopVariable = '....';
element.addEventListener('focus', (function(innerLoopVariable) {
return function() {
// your code here
// access to the loop variable through "innerLoopVariable"
}
})(myLoopVariable), false);