Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

How to wrap an input file in a form using javascript?

I have a function:-

function resetInputFile(elem) {
    elem.wrap('<form>').closest('form').get(0).reset();
    elem.unwrap();
}

I call the function like this:-

resetInputFile(document.queryElement('#image'));

How can I convert the LOCs

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

elem.wrap('<form>').closest('form').get(0).reset();
elem.unwrap();

into pure javascript?

I want to implement the logic in ReactJS, but I don’t want to use jquery with ReactJS. So I need to do it with pure javascript.

>Solution :

You can try something like:

function resetInputFile(elem) {
  const form = document.createElement('form');
  const parent = elem.parentNode;

  parent.insertBefore(form, elem);
  form.appendChild(elem);
  form.reset();
  parent.insertBefore(elem, form);
  parent.removeChild(form);
}

function resetFileInput() {
  resetInputFile(document.querySelector('#image'));
}
<input type="file" id="image">
<button onclick="resetFileInput()">Reset File Input</button>
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading