onclick event function in JavaScript
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:
- Do not use
onclick="javascript:myfunc()"
. Only use thejavascript:
prefix inside thehref
attribute of a hyperlink:<a href="javascript:myfunc()">
. - You don't have to end with a semicolon.
onclick="foo()"
andonclick="foo();"
both work just fine. - Event attributes in HTML are not case sensitive, so
onclick
,onClick
andONCLICK
all work. It is common practice to write attributes in lowercase:onclick
. note that javascript itself is case sensitive, so if you writedocument.getElementById("...").onclick = ...
, then it must be all lowercase.
click() is a reserved word and already a function, change the name from click() to runclick() it works fine
Try this
<input type="button" onClick="return click();">button text</input>