I want to create a set of buttons, each responding with a different message to an 'onclick' event. It would be easiest to create them in a loop - unfortunately, I'm not sure how to pass the unique data to the function object when I create them. The functions I'm creating below all print "Hello, I'm 6!". What's the right way to do this in Javascript?
function makeButtonTest(rootEle)
{
for (var i = 1; i <= 5; ++i)
{
var d = document.createElement("div");
rootEle.appendChild(d);
d.onclick = function(){
alert("Hello, I'm " + i + "!");
};
d.appendChild(document.createTextNode("Number " + i));
}
}