Last active
November 3, 2017 14:15
-
-
Save loilo/52858fc1086727ba1d6f89e4ae95ca75 to your computer and use it in GitHub Desktop.
Reset a single form element
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
// 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