html – JavaScript button onclick not working
html – JavaScript button onclick not working
Note to other developers coming across this, you can run into this if you use a reserved method names e.g. clear
.
<!DOCTYPE html>
<html>
<body>
<button onclick=clear()>Clear</button>
<button onclick=clear2()>Clear2</button>
<script>
function clear() {
alert(clear);
}
function clear2() {
alert(clear2);
}
</script>
</body>
</html>
How about this?
<button id=hellobutton>Hello</button>
<script>
function hello() {
alert(Hello);
}
document.getElementById(hellobutton).addEventListener(click, hello);
</script>
P.S. You should place hello() above of the button.
html – JavaScript button onclick not working
Ran into this problem myself so I can confirm somethings not right. The difference is that I am generating the DOm Element at runtime. Replacing onclick
with onmousedown
seemed to do the trick if you cant find a place to addEventListener
in your code.