Last active
January 1, 2016 08:59
-
-
Save ronaldronson/8121703 to your computer and use it in GitHub Desktop.
Clear all form inputs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* Clear form data. | |
* | |
* @param HTMLFormElement | |
*/ | |
function clearForm(form) { | |
var l = form.elements.length, o, list = { | |
"checkbox": {prop: "checked", val: false}, | |
"radio": {prop: "checked", val: false}, | |
"select-one": {prop: "selectedIndex", val: null}, | |
"select-multi": {prop: "selectedIndex", val: null}, | |
}; | |
for (;l--;) { | |
o = list[form[l].type]; | |
o ? (form[l][o.prop] = o.val) : (form[l].value = ''); | |
} | |
} | |
clearForm(document.forms[0]); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment