Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how to unselect files in html forms?

I have a simple php file upload form, something like this:

<form action="upload_file.php" method="post" onsubmit="return validateForm()" enctype="multipart/form-data">
<label for="file">Files:</label>
<input type="file" name="file[]" id="file"><button type="button">Remove File</button>
<input type="file" name="file[]" id="file"><button type="button">Remove File</button>
<input type="submit" name="submit" value="Submit">
</form>

and I would like to add a function the Remove File button in order to unselect the selected file. Is that possible ?

Thanks for the help.

like image 844
user3002293 Avatar asked Nov 18 '13 20:11

user3002293


People also ask

How do I unselect a selected file?

You can use Ctrl+Arrow to move among selected items. Ctrl+Space will select/deselect.

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 I hide no file chosen in HTML?

Hide the input with css, add a label and assign it to input button. label will be clickable and when clicked, it will fire up the file dialog. Then style the label as a button if you want. This even works in IE9, where you can't hide the file input and click it from JavaScript.

How do you change no file chosen text in input type file in HTML?

Remove the value('No file chosen'). Use . addClass() method to add the class which removes the value “No file chosen”.

How do I make unselectable text in HTML?

Welcome to a quick tutorial on how to make unselectable text in HTML. So you have a not-so-top-secret website that you want to protect? Stop people from highlighting and copying the text? In CSS, simply add the user-select: none property. In Javascript, document.getElementById ("TARGET").onselectstart = function () { return false; }

How do I select/unselect files and folders?

Under the Edit menu there are a number of special commands for selecting files and folders. The Select and Unselect options show a select/unselect dialog where you select files using filters. You can enter a wildcard filter to select/unselect files, eg. "*.jpg *.tiff" for all jpg and tiff files. Separate multiple filers with a space.

How do you define a file select field in HTML?

Definition and Usage. The <input type="file"> defines a file-select field and a "Browse" button for file uploads. To define a file-select field that allows multiple files to be selected, add the multiple attribute. Tip: Always add the <label> tag for best accessibility practices!

How do I select multiple files in HTML?

Definition and Usage The <input type="file"> defines a file-select field and a "Browse" button for file uploads. To define a file-select field that allows multiple files to be selected, add the multiple attribute. Tip: Always add the <label> tag for best accessibility practices!


1 Answers

You'll have to add IDs to make it easier, otherwise you'll be traversing nodes and you won't like that.

<form action="upload_file.php" method="post" onsubmit="return validateForm()" enctype="multipart/form-data">
    <label for="file">Files:</label>
    <input id="file1" type="file" name="file[]" />
    <button id="rmv1" type="button">Remove File</button>

    <input id="file2" type="file" name="file[]" />
    <button id="rmv2" type="button">Remove File</button>

    <input type="submit" name="submit" value="Submit">
</form>

Then add the javascript to restore default values:

document.getElementById('rmv1').onclick = function() { 
    var file = document.getElementById("file1");
    file.value = file.defaultValue;
}

(change rmv1 into rmv2 and file1 into file2 for the other button)

like image 96
Sterling Archer Avatar answered Sep 27 '22 18:09

Sterling Archer