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?
We can clear the file input with JavaScript by setting the value property of the file input to an empty string or null .
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.
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.
This should work:
$imageClear.on('click', function() {
$imageFile.val('');
});
Clear file input with jQuery
$("#fileInputId").val(null);
Clear file input with JavaScript
document.getElementById("fileInputId").value = null;
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 = ""
this.inputRef = React.createRef()
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/
get input id and put val is empty like below: Note : Dont put space between val empty double quotes val(" ").
$('#imageFileId').val("")
By Setting $("ID").val(null)
, worked for me
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']"));
this code works for all browsers and all inputs.
$('#your_target_input').attr('value', '');
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With