Submitting A HTML Form Using JavaScript

In order to submit a form using an event you need to run a click event on any submit button in the form. Take the following form.

 <form method="post" onsubmit="alert('Form submitted!'); return false;">
    <input type="text" name="testvalue" value="" />
    <input type="submit" name="submit" id="submit" value="Submit" />
</form>

To run a submit event using JavaScript we just find the submit button via the id and run click() against it.

document.getElementById('submit').click();

That's it!

Share:

  • Add news feed
  • Bookmark this on Delicious

Comments

Post new comment

The content of this field is kept private and will not be shown publicly.
  • Allowed HTML tags: <a> <em> <strong> <cite> <code> <ul> <ol> <li> <dl> <dt> <dd> <h2> <h3> <h4> <h5> <h6> <pre> <span> <p> <br />
  • Syntax highlight code surrounded by the {syntaxhighlighter SPEC}...{/syntaxhighlighter} tags, where SPEC is a Syntaxhighlighter options string or "class="OPTIONS" title="the title".

More information about formatting options

By submitting this form, you accept the Mollom privacy policy.