Take a look at this JSFiddle example I just put together: http://jsfiddle.net/BshLK/1/
The problem with your code sample is that i
is undefined by the time your event handler runs as it's only has it's value whilst the loop is running. To correct it, firstly you need to access the element you've called the event on and secondly you need to get use getAttribute()
to get the attribute value.