Skip to content

Instantly share code, notes, and snippets.

@randell
Created April 21, 2012 12:56
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save randell/2436981 to your computer and use it in GitHub Desktop.
Save randell/2436981 to your computer and use it in GitHub Desktop.
Clear input values inside a div using JavaScript
// From http://stackoverflow.com/a/1500073/106778
function clearChildren(element) {
for (var i = 0; i < element.childNodes.length; i++) {
var e = element.childNodes[i];
if (e.tagName) switch (e.tagName.toLowerCase()) {
case 'input':
switch (e.type) {
case "radio":
case "checkbox": e.checked = false; break;
case "button":
case "submit":
case "image": break;
default: e.value = ''; break;
}
break;
case 'select': e.selectedIndex = 0; break;
case 'textarea': e.innerHTML = ''; break;
default: clearChildren(e);
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment