I have a webpage which I'm writing an extension for which detects 1 or more table/div sections and I want to insert a button into each to then call a function and pass in dynamic parameters to another function. The problem I'm running into is the dynamic values are always the last in the loop even though the button IDs appear correct in the page source (dynamic). Simplified code extract below:
for (var milestone_id in all_milestones) {
milestone = all_milestones[milestone_id];
button = document.createElement("input");
button.type = "button";
button.setAttribute("id", "callout_" + milestone_id);
button.setAttribute("style", "float: right");
button.value = "Create/Edit Details";
button.onclick = function() {open_browser(award_id, milestone_id, milestone["date"], env)};
milestone["class"].appendChild(button);
}
Current behaviour - buttons are successfully injected in with unique IDs (callout_{milestone_id}) but when invoking the function (open_browser) the milestone_id is static // the last value of milestone in the array.
Desired behaviour - as above but ensuring the correct dynamic values are sent through as per the time that the button is injected into the form.