Ask coding questions

← Back to all posts
Can’t get onclick attr to work
Giothecoder

in file public/index.html I use a div element with the onclick attribute to act as a button, but when I click on the button, nothing happens. It’s supposed to get rid of the button and the input element and then add a canvas element that covers the entire screen, but nothing happens. Anyone care to tell me what I’m doing wrong?

Thanks,
G

Update: I have found on the browser I’m using this warning:
[Violation] 'click' handler took <N>ms where <N> is around 155 to 521. I don’t actually know if that’s any help, but yeah there you go.

Answered by MrEconomical [earned 5 cycles]
View Answer
Voters
Jacog28
Giothecoder
Comments
hotnewtop
MrEconomical
  1. dont use onclick, use element.addEventListener in your javascript
  2. if you want a button, use a <button> not a <div>
Giothecoder

@MrEconomical
1. Why?
2. Aw..

MrEconomical

@Giothecoder its just better

Giothecoder

@MrEconomical
much confusion
How?

MrEconomical

@Giothecoder easier to debug I guess

Giothecoder

@MrEconomical alright I’ll try it one sec...

Giothecoder

@MrEconomical ok. So. Tried that. Now I’m not getting even a warning at all, so I think the problem isn’t with my click listening anymore, but with how I get the new element on the page, what do you think?

Edit: it has been fixed, sorry about not marking you as the answer, I forgot but there you go. Ty!