Skip to content

Onclick attribute for button create inside Javascript not working

I’m stuck in a problem. The onclick property for the button created inside Javascript is not working Here is my code:
Click here
Reply function: Click here

Answer

try this

var button = document.createElement('button');
button.className = 'btn btn-outlline-primary';
button.value = 'Reply';
button.setAttribute("onclick", "YOURFUNCTIONNAME()");
div.appendChild(button);

you can give your button an onclick funtion with setAttribute