0

Defining 'onclick' when appending a button in JS

var button1 = document.createElement("button"); button1.innerHTML = "TEST BUTTON"; button1.onclick = alert("Knock-knock"); document.body.appendChild(button1); Why do I get alerted just when visiting/updating the page, but not when clicking on the button? And what should I do to achieve desired behavior (getting alerts only when clicking on the button)? Thank you in advance for answering.

26th Feb 2022, 10:08 PM
Alex Snaidars
Alex Snaidars - avatar
0 odpowiedzi