Exit
  • Global community
    • Language:
      • Deutsch
      • English
      • Español
      • Français
      • Português
  • 日本語コミュニティ
  • 한국 커뮤니티
0

onclick or onsubmit which is better?

Explorer ,
Apr 18, 2006 Apr 18, 2006
Hello,


Adding JS validation onclick of a submit button or adding JS to form onsubmit, which is better?. What is Adv and Dis?.


<form name="frm" method="post">
<input type="Submit" name="Submit" value="Submit" onclick="return JSCheck(this.form)">
</form>

OR

<form name="frm" method="post" onsubmit="return JSCheck(this.form)">
<input type="Submit" name="Submit" value="Submit">
</form>

Thanks

TOPICS
Advanced techniques
2.0K
Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
Apr 18, 2006 Apr 18, 2006
In this kind of programming there are no hard and fast rules. I don't think there is a significant difference between the two. Which one you choose depends on how creatively you put the function JSCheck() together.

However, i prefer the second. I get the feeling the button is 'overloaded', as it has to respond to two events: submit the form and run the script.

Also, if the Javascript performs form validation, then it will only be natural for it to be called by onSubmit. Imagine that the result of the form validation, return JSCheck(this.form), is a boolean. If it is true, the form is submitted; otherwise, not. In these circumstances the code onSubmit="true;" will be more natural than the code onClick="true;" .

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
LEGEND ,
Apr 18, 2006 Apr 18, 2006
LATEST
onsubmit is better because you can submit a form without clicking anything.
Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Resources