onclick event not working in JavaScript

2019-01-11 08:54发布

I have some JavaScript code in an HTML page with a button. I have a function called 'click()' that handles the onClick event of the button. The code for the button is as follows:

<input type="button" onClick="click()">button text</input>  

The problem is that when the button is clicked, the function is not called. What am I doing wrong here?
Thanks

9条回答
再贱就再见
2楼-- · 2019-01-11 09:08

Try fixing the capitalization. onclick instead of onClick

Reference: Mozilla Developer Docs

查看更多
闹够了就滚
3楼-- · 2019-01-11 09:10

Yes you should change the name of your function. Javascript has reserved methods and onclick = >>>> click() <<<< is one of them so just rename it, add an 's' to the end of it or something. strong text`

查看更多
仙女界的扛把子
4楼-- · 2019-01-11 09:13
<script>
//$(document).ready(function () {
function showcontent() {
        document.getElementById("demo22").innerHTML = "Hello World";
}
//});// end of ready function
</script>

I had the same problem where onclick function calls would not work. I had included the function inside the usual "$(document).ready(function(){});" block used to wrap jquery scripts. Commenting this block out solved the problem.

查看更多
孤傲高冷的网名
5楼-- · 2019-01-11 09:14

Today this also happened to me. The function name maybe conflicts with keywords. My case is scrape(). I change the function name, everything works fine.

查看更多
等我变得足够好
6楼-- · 2019-01-11 09:18

Check you are calling same function or not.

<script>function greeting(){document.write("hi");}</script>

<input type="button" value="Click Here" onclick="greeting();"/>
查看更多
爷的心禁止访问
7楼-- · 2019-01-11 09:22

click() is a reserved word and already a function, change the name from click() to runclick() it works fine

查看更多
登录 后发表回答