Since (as far as I know) the click handler cannot accept arguments, you need to make a function with a variable in the scope, which should look something like this:
for( ... ) { elementArray[i].click( onElementClick(i) ); ... } function onElementClick( id ){ return function() { console.log("element clicked - selectedElementIndex = " + id ); } }
source share