+ 9

My onclick atribugte doesn't work. Why?

<!DOCTYPE html> <html> <head> <title>Page Title</title> </head> <body> <button onclick="click()">OK</button> <script> function click(){ alert("Hello"); } </script> </body> </html>

24th Dec 2016, 7:37 AM
Aydin
4 Answers
+ 12
Thanks everybody. When i changed clik it really worked.
25th Dec 2016, 8:33 AM
Aydin
+ 7
So this is a very good question and a lot of people don't know it but the name click () is already reserved so you aren't allowed to use it just use anything else for the name of the function like sayHello () and it will work hope this helps ~Kamil
24th Dec 2016, 8:35 PM
Kamil
Kamil - avatar
+ 4
'Cause you put script after tag? (...or try removing parenthesis from onclick)
24th Dec 2016, 7:39 AM
Valen.H. ~
Valen.H. ~ - avatar
0
Try declaring the function script before the button. Perhaps put the script between the head tags.
24th Dec 2016, 7:54 AM
Antek