<h3>Form onsubmit event </h3>
<p>For any final checking you can't easily do while the form is being filled out, the form's "submit" event provides the means for checking everything before the submission happens.</p>
<form action="#" id="theForm" name="firstForm">
<label for="age">Favorite Color: </label>
<input type="text" name="color" size="8"/><br/>
<label for="age">Favorite Food: </label>
<input type="text" name="color" size="8"/><br/>
<button type="submit" name="submit">Submit</button>
</form>
JavaScript
var f = document.forms[0];
/* This handler will run when you submit the form */
f.addEventListener("submit", function (e) {
/// Here we can do whatever we want with the form
// and its elements.
for (var i=0; i< f.elements.length; i++){
console.log(f.elements[i].value);
}
// if things aren't right, I can cancel the form
// submission right here:
e.preventDefault();
});
Please Whitelist JSFiddle in your content blocker.
Help keep JSFiddle free for always by one of two ways:
Whitelist JSFiddle in your content blocker (two clicks)
Go PRO and get access to additional PRO features →
Join the 4+ million users, and keep the JSFiddle dream alive.
Ad-free
All ads in the editor and listing pages are turned completely off.
Use pre-released features
You get to try and use features (like the Palette Color Generator) months before everyone else.
Fiddle collections
Sort and categorize your Fiddles into multiple collections.
Private collections and fiddles
You can make as many Private Fiddles, and Private Collections as you wish!
Console
Debug your Fiddle with a minimal built-in JavaScript console.