I have this method for creating new buttons:
function createButton(buttonId, buttonClass, clickEvent) {
var button = document.createElement("button");
button.id = buttonId;
button.className = buttonClass;
button.onclick = function(){
clickEvent;
};
return button;
}
That I call in this way:
var editButton = createButton(
"edit-" + string + "-allow",
"btn btn-sm btn-primary btn-edit-lg",
allowEdit("edit-" + string + "-allow")
);
But checking on the element inspector the onclick property is not added
What’s the problem?
2
Answers
You need to pass the function name and the parameter. Otherwise it will execute when assigned
Also I suggest to use eventListener instead of onclick
If the button click is the same and only the parameter changes; I suggest delegation:
you can try