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 f
Try this
<input type="button" onClick="return click();">button text</input>
click() is a reserved word and already a function, change the name from click() to runclick() it works fine
I suggest you do:
<input type="button" value="button text" onclick="click()">
Hope this helps you!
Check you are calling same function or not.
<script>function greeting(){document.write("hi");}</script>
<input type="button" value="Click Here" onclick="greeting();"/>
Two observations:
You should write
<input type="button" value="button text" />
instead of
<input type="button">button text</input>
You should rename your function. The function click()
is already defined on a button (it simulates a click), and gets a higher priority then your method.
Note that there are a couple of suggestions here that are plain wrong, and you shouldn't spend to much time on them:
onclick="javascript:myfunc()"
. Only use the javascript:
prefix inside the href
attribute of a hyperlink: <a href="javascript:myfunc()">
.onclick="foo()"
and onclick="foo();"
both work just fine.onclick
, onClick
and ONCLICK
all work. It is common practice to write attributes in lowercase: onclick
. note that javascript itself is case sensitive, so if you write document.getElementById("...").onclick = ...
, then it must be all lowercase.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`