Why does my function work incorrectly?
Sorry for not giving a clear title, because I don't know why my script
doesn't work.
var all=[];
function People(name){
this.name=name;
this.func=function(){alert(this.name)};
all.push(this);
};
var person1=new People('Peter');
for(i=0;i<all.length;i++){
var newBtn=document.createElement('input');
document.body.appendChild(newBtn);
newBtn.type='button';
newBtn.value=all[i].name;
newBtn.onclick=all[i].func; // why doesn't is say "Peter" ?
};
Btw, is there a better way to achieve my goal?
No comments:
Post a Comment