2

I need to clear/reset input type file within Vue.js 2 Before I've used jQuery method clone:

var input = $(div).find("input[type='file']");
input.replaceWith(input.val('').clone(true));

How can I clear this input? I can't use v-model for input because it doesn't work in vue 2

7
  • adding a <input type='reset'/> can do this. Commented Dec 13, 2017 at 7:04
  • @GhanshyamSingh this is not an option for my usecase, I have a lot of fields in this form. Commented Dec 13, 2017 at 7:19
  • 3
    you can use vuejs.org/v2/api/#ref ref vue js attribute to hold that node reference and then when you want to clear you can do => vm.$refs.fileupload.value= null; Commented Dec 13, 2017 at 8:37
  • @HardikSatasiya I've tested this - not working for IE 11, and default browser on Android 4.4 Commented Dec 13, 2017 at 9:26
  • are you adding jquery in your project or just vue, if you are adding jquery then you can do same stuff as you do before but now input can be var input = $(vm.$refs.fileupload) Commented Dec 13, 2017 at 9:35

4 Answers 4

2

it works.

<input type="file" ref="myFileInput"/>


this.$refs.myFileInput.value = '';

Reference : https://jj.isgeek.net/2018/02/how-to-reset-file-input-with-vuejs/

Sign up to request clarification or add additional context in comments.

Comments

1

My old site used your same method when I was using AngularJS. I'm currently converting to use Vue and ran into the same issue. Here is what I did to solve my problem.

fileInput.file = null

or

$('#fileInput')[0].file = null

Comments

0

Try this:

reset_document()
{this.instructions_file_document = "";
$("input[type=file]").val(null);}

Comments

0

I tried all of the above, but my project had several errors. What solved my problem was:

<input type="file" ref="inputFile"/>

this.$refs.inputFile.reset();

This code above resets the field values.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.