Steven Spielberg
Steven Spielberg

Reputation:

how to reset the value of file controls in html

i used file control in a html page i want to remove the file location from control when user click on reset button.

$("#control").val("");

if i run this code he not work in Chrome but in Firefox

what i do to unselect the file who is not selected in file upload control.

how i can reset the controls without reset the whole form. are any thing exist to do this.

Upvotes: 2

Views: 4791

Answers (4)

Hassan Ali Shahzad
Hassan Ali Shahzad

Reputation: 2724

Try this JS code without any external library:

var file = document.getElementById("file1");
    file.value = file.defaultValue;

Upvotes: 0

Pranav Bhatt
Pranav Bhatt

Reputation: 745

If you using jQuery then following also can be utilize.

$('"#control"').prop('value', '');

Upvotes: -1

ksg91
ksg91

Reputation: 1299

The other solution didn't work me. I used the following code and it worked for me.

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

Upvotes: 5

Prutswonder
Prutswonder

Reputation: 10064

According to this article, you can use the following JavaScript method to clear the HTML input file control's value:

function clearFileInputField(tagId) {
    document.getElementById(tagId).innerHTML = 
                    document.getElementById(tagId).innerHTML;
}

Or, if refactored in jQuery, this should work as well:

$("#control").html($("#control").html());

Upvotes: 3

Related Questions