I'm creating a template for handling form buttons.
The idea is on first run of a function (when i lock a form), said function will create onClick listeners for buttons to edit, cancel, delete, etc that will trigger their respective functions (hardcoded in global scope)
The part of the code that's giving me headaches is:
//Build standard button actions on first call...
if(window[form_elem_tag + "funcs"] == 1) return;
window[form_elem_tag + "funcs"] = 1;
var b2n_act = 'edit';
var func_name = form_elem_tag + "_" + b2n_act;
var b2n_name = form_elem + "_btn_" + b2n_act;
$(b2n_name).on("click",function(){
console.log("calling " + func_name);
window[func_name]();
});
console.log("funcName created: " + func_name);
var b2n_act = 'cancel';
var func_name = form_elem_tag + "_" + b2n_act;
var b2n_name = form_elem + "_btn_" + b2n_act;
$(b2n_name).on("click",function(){
console.log("calling " + func_name);
window[func_name]();
});
console.log("funcName created: " + func_name);
In the above scenario, i created the actions for the EDIT button , and the the CANCEL button.
The problem im having is that the when i click the EDIT button the listener that fires is of the CANCEL button..
console logged:

and when i reverse the order, it becomes apparent that what ever is the last listener I create , its function seems to fire for all buttons..
What's wrong with my code?
form_elem_tag? A string? what are (some of) the possible values?