dynamically create buttons with different event calls Javascript
I have a problem in the following function,I am trying to create buttons
dynamically based on details from the results variable,The button are
created and an event is attached but it seems each button has the exact
same event attached.I need for the address variable to be different for
each event attached to a button and for that button then to be added to
replace text in my macField variable.I know i am close but have been
looking at this for a good while and cannot seem to pinpoint where exactly
i am going wrong ,If some with a fresh eye and more experienced could help
me out here I'd be grateful.
function (results) {
var r;
var x, i;
var btn;
for (i = 0; i < results.length; i++) {
app.display("Paired to" + results[i].name +
results[i].address);
x = document.getElementById("message2");
r = results[i].address;
w = document.getElementById('macField').value;
btn = document.createElement('input');
btn.onclick = function () {
document.getElementById('macField').value = r;
};
btn.setAttribute('type', 'button');
btn.setAttribute('name', 'sal' + [i]);
btn.setAttribute('id', 'Button' + [i]);
btn.setAttribute('value', results[i].name);
appendChild(btn);
}
},
function (error) {
app.display(JSON.stringify(error));
}
No comments:
Post a Comment