HTML submit button onclick code

An HTML button is created using the following HTML code:

<input type='submit' name="submit" value="Post Form" />

Or using the button tag like this:

<button type="submit" >Post the form</button>

In both the cases, pressing the button will submit the parent form without the need for handling the onclick event separately.

If you want to validate the form before submitting, the best event handler would be the onsubmit event of the form. Example:

Returning false from the onsubmit event handler of the form stops the form being submitted.

So the submit button has a meaningful existance only within a form

If, you want a plain simple button that triggers an event when the button is pressed/clicked, use the button tag with type=submit like this

<button type="button" onclick="my_button_click_handler">Click this button</button>
<script>
function my_button_click_handler()
{
alert('Button Clcked');
}
</script>