Skip to content
Advertisement

Button created doesn’t have onclick

Here’s my HTML:

JavaScript

and my javascript:

JavaScript

When my button is created the onclick event is fired automatically (I get hello printed in the console without clicking on the button) and then doesn’t work. When I inspected the page I realized that my button didn’t have an onclick anymore (just class and value)

I can’t figure out why so any help would be greatly appreciated. Thanks.

Advertisement

Answer

onclick needs to be a function. Please see the example.

JavaScript
JavaScript
User contributions licensed under: CC BY-SA
1 People found this is helpful
Advertisement