I am trying to create buttons dynamically and assining an alert function for each one on a click event.Here is the javascript function as:
function GetTabs(tabObj) {
for (var t = 0; t < tabObj.views.view.length; t++) {
var podObjlen = '';
var element = document.createElement('input');
element.id = "btn" + t;
element.setAttribute("type", "button");
element.setAttribute("value", "Click");
element.setAttribute("name", "button");
podObjlen = tabObj.views.view[t].pod.length;
document.body.appendChild(element);
document.getElementById('btn' + t).onclick = (this, "onclick", function {
alert(podObjlen);
});
}
}
No matter what button i click the alert always shows me the last value from the loop for all the buttons i.e length value is being assigned to all the alerts. It isn't assigning independent values for each alert of a button. I have tried several ways like:
element.getElementById('btn' + t).onclick = (this, "onclick", function {
alert(podObjlen);
});
element.getElementById('btn' + t).onclick = new function () {
alert(podObjlen);
}
(Here it shows me proper values in alertbox but i get alert one after the other continously but the alert fails to fire when i actually click a button i.e the alert fires the moment my buttons are rendered. Hope i am being clear. What i want is i need to get the actual value to be assingned to alert of each button when the loop is being run.