Javascript Code:
function createdivs()
{
var i = 1;
$.ajax({ url: 'displaycontent.php',
data: {action:'test'},
type: 'post',
datatype: 'json',
success: function(output) {
var a = JSON.parse(output);
var b = a.length;
var i;
for (i = 0; i < b;i++){
console.log(a[i]['idi']);
console.log(a[i]['pgmname']);
console.log(a[i]['comp']);
console.log(a[i]['datephp']);
if (a[i]['idi'] != 0){
for (var j=0 ;j < 1;j++)
{
var editp = document.createElement("button","span");
document.getElementById('contentdisplay'+i).appendChild(editp);
editp.className = "glyphicon glyphicon-pencil";
editp.id = "editprogramsbutton"+i;
$("#editprogramsbutton"+i).click(function(){
editp.value = i;
alert(i);
});
}
}
}
}
});
}
Suppose if the loop runs for 3 times, I can create three buttons with id's as editprogramsbutton0, editprogramsbutton1, editprogramsbutton2. The problem is when I alert using
$("#editprogramsbutton"+i).click(function(){
editp.value = i;
alert(i);
});
I always get the value as 3 no matter which button I press. I know that the value of 'i' is set to 3 and it'll display the last updated value whenever I press the button. I want values like "0" when I press "editprogramsbutton0", "1" when I press "editprogramsbutton1" and so on.. Can someone please explain the logic of how to do this with possible changes in the code. Thank you in advance.