So I am creating buttons in a response to a succcessful AJAX call, and each button needs to call a function with that specific dynamic info. Here is how I am doing it right now:
for(var i = 0; i < variableNames.length; i++) {
var getInvButton = document.createElement('button');
getInvButton.id = 'getInventoryButton';
console.dir('Add a button with this ID: ' + variableNames[i]);
getInvButton.addEventListener("click", function(charName, membershipName) {
console.dir('Test button click: ' + charName);
getInventoryVar(charName, membershipName);
} (variableNames[i],
result.Response.characters.data[variableNames[i]].membershipId), false);
}
I can access my variables properly, and the getInventoryVar is called with the right parameters for each button. However, this function is being executed immediately upon it being added.
getInvButtonhas the same id for allvariableNames. Second: Do you add the buttons anywhere to your DOM?