I am adding a button dynamically to HTML as shown below.

When clicking that button I want to call a JavaScript function.

var but = document.createElement("button");

but.value="delete row";

but.setAttribute("onclick","callJavaScriptFunction()");
// this is not working

but.onclick="callJavaScriptFunction()"
// this is also not working

document.getElementById("but").onclick="callJavaScriptFunction()"
// this is also not working

but.id="but"+inc;

How can this be resolved?

Edit
Report