<button onclick="hello()">Hello</button>
<script>
function hello() {
alert('Hello');
}
</script>
This is my code. But it's not working. When I click on the button nothing happens.
<button onclick="hello()">Hello</button>
<script>
function hello() {
alert('Hello');
}
</script>
This is my code. But it's not working. When I click on the button nothing happens.
I had a similar issue. I had
child.js
and acommon.js
files. In my case, My HTML file was using both the JS files and both of them had a function with the same name,and also
After I remove one of these my code works fine and onclick started working. hope this helps!
How about this?
P.S. You should place hello() above of the button.
Ran into this problem myself so I can confirm something's not right. The difference is that I am generating the DOm Element at runtime. Replacing
onclick
withonmousedown
seemed to do the trick if you can't find a place toaddEventListener
in your code.There is no problem with your code.. run this snippet
and if you want to alert this on window load. change your code like this way
Note to other developers coming across this, you can run into this if you use a reserved method names e.g.
clear
.