When I use the current for loop value in my .on("click",function(){})
the value actually used is the break condition value of the for loop
var arr =["S","d","f"];
for(i=0;i<arr.length;i++)
{
$("#sng"+i).on("click",function(){
console.log(i); //is always 3
console.log(arr[i]); //is undefined as array doesnt have index 3
});
If the array has 4 elements then console.log(i)
is always 5 etc.
The selector works properly as thats how i created the div using for loop and assigning id=sng+i
.
Why is this happening and how do i use the current i value.
My elements are all created dynamically.