Skip to content

Instantly share code, notes, and snippets.

@loilo
Last active November 3, 2017 14:15
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save loilo/52858fc1086727ba1d6f89e4ae95ca75 to your computer and use it in GitHub Desktop.
Save loilo/52858fc1086727ba1d6f89e4ae95ca75 to your computer and use it in GitHub Desktop.
Reset a single form element
// Resets a given form element
function resetInput (input) {
// 1. Remember the <input> position by grabbing sibling and parent
const next = input.nextSibling
const parent = input.parentNode
// 2. Create a temporary <form> element, put the <input> there and reset that form
const tmpForm = document.createElement('form')
tmpForm.appendChild(input)
tmpForm.reset()
// Additional bugfix for IE11 who does not clear the FileList on form reset
if (input.type === 'file') {
input.value = ''
}
// 3. Put the <input> back to its original position
parent.insertBefore(input, next)
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment