Skip to content

Instantly share code, notes, and snippets.

@guyht
Created March 22, 2011 13:02
Show Gist options
  • Save guyht/881177 to your computer and use it in GitHub Desktop.
Save guyht/881177 to your computer and use it in GitHub Desktop.
<form id="my-form">
<input type="text" name="in" value="some data" />
<button type="submit">Go</button>
</form>
<script>
// Should only be triggered on first page load
alert('ho');
function processForm() {
/* do what you want with the form */
// Should be triggered on form submit
alert('hi');
// You must return false to prevent the default form behavior
return false;
}
var form = document.getElementById('my-form');
if (form.attachEvent) {
form.attachEvent("submit", processForm);
} else {
form.addEventListener("submit", processForm);
}
</script>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment