I have 3 spans, valued 0, 1, and 2. When you click on one, I want to alert its value. With my current code, they all alert "length." I don't know what I'm doing wrong
<style>
.text{
width:20px;
height:20px;
border:1px solid black;
}
</style>
<span class='text'>0</span>
<span class='text'>1</span>
<span class='text'>2</span>
<script>
var textSpans=document.querySelectorAll('span.text');
for (i in textSpans)
{
textSpans[i].onclick=function() {alert(i);};
}
</script>
here is a fiddle