Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to clear File Input

Tags:

jquery

Below is part of the jquery code I have which displays the file input and a "Clear File" button.

var $imagefile = $('<input />').attr({
    type: 'file',
    name: 'imageFile',
    class: 'imageFile'
});

$image.append($imagefile);

var $imageclear = $('<input />').attr({
    type: 'button',
    name: 'imageClear',
    class: 'imageClear',
    value: 'Clear File'
});

$image.append($imageclear);

Now the reason I have the "Clear File" button is because if you click on the button, then it will clear anything that is in the file input. How do I code it so that it actually clears the file input when I click on the "Clear File" button?

like image 981
user1181690 Avatar asked Mar 08 '12 12:03

user1181690


People also ask

How do I empty a file input?

We can clear the file input with JavaScript by setting the value property of the file input to an empty string or null .

How do you clear form values?

You can easily reset all form values using the HTML button using <input type=”reset”> attribute. Clicking the reset button restores the form to its original state (the default value) before the user started entering values into the fields, selecting radio buttons, checkboxes, etc.

How do you clear the selected file from Fileupload control in React?

To reset a file input's value in React, we can set the value property of the file input to an empty string. We call the useRef hook to create a ref object. And we assign that to the file input with the ref prop. Next, we add a button that calls the reset function when we click it.


8 Answers

This should work:

$imageClear.on('click', function() { 
    $imageFile.val(''); 
});
like image 149
Guillaume Poussel Avatar answered Oct 03 '22 23:10

Guillaume Poussel


Clear file input with jQuery

$("#fileInputId").val(null);

Clear file input with JavaScript

document.getElementById("fileInputId").value = null;
like image 34
Arvin Jayanake Avatar answered Oct 03 '22 21:10

Arvin Jayanake


for React users

e.target.value = ""

But if the file input element is triggered by a different element (with a htmlFor attribute) - that will mean u don't have the event

So you could use a ref:

at beginning of func:

const inputRef = React.useRef();

on input element

<input type="file" ref={inputRef} />

and then on an onClick function (for example) u may write

inputRef.current.value = "" 
  • in React Classes - same idea, but difference in constructor: this.inputRef = React.createRef()
like image 33
Shani Kehati Avatar answered Oct 03 '22 21:10

Shani Kehati


This is the method I like to use as well but I believe you need to add the bool true parameter to the clone method in order for any events to remain attached with the new object and you need to clear the contents.

    var input = $("#fileInput");

    function clearInput() {
        input = input.val('').clone(true);
    };

https://api.jquery.com/clone/

like image 28
meditari Avatar answered Oct 03 '22 21:10

meditari


get input id and put val is empty like below: Note : Dont put space between val empty double quotes val(" ").

 $('#imageFileId').val("")
like image 23
Nagnath Mungade Avatar answered Oct 03 '22 22:10

Nagnath Mungade


By Setting $("ID").val(null), worked for me

like image 44
Pranav Kulshrestha Avatar answered Oct 03 '22 23:10

Pranav Kulshrestha


Another solution if you want to be certain that this is cross-browser capable is to remove() the tag and then append() or prepend() or in some other way re-add a new instance of the input tag with the same attributes.

<form method="POST" enctype="multipart/form-data">
<label for="fileinput">
 <input type="file" name="fileinput" id="fileinput" />
</label>
</form>

$("#fileinput").remove();
$("<input>")
  .attr({
    type: 'file',
    id: 'fileinput',
    name: 'fileinput'
    })
  .appendTo($("label[for='fileinput']"));
like image 25
Iceman Avatar answered Oct 03 '22 21:10

Iceman


this code works for all browsers and all inputs.

$('#your_target_input').attr('value', '');
like image 32
reza laki Avatar answered Oct 03 '22 22:10

reza laki