how does one blank a file input in older versions of IE?

J

Jake Barnes

At a certain point in my code, I need to blank the value in a file
input. The situation comes up when a user first thinks they want to
upload something from their harddrive, but then they change their mind
and decide to choose a file from an online archive. I thought I could
get a reference to the input and then set value to blank, but that only
works in FireFox. For IE, I only got this to work by setting innerHTML
to blank:

if (document.getElementById &&
document.getElementById(idOfFileInputToBeMadeBlank)) {
var referenceToFileInput =
document.getElementById(idOfFileInputToBeMadeBlank);
referenceToFileInput.value = "";
referenceToFileInput.innerHTML = "";
}

However, this will probably break in old versions of IE. So what works
for older versions of IE?
 
R

Randy Webb

Jake Barnes said the following on 9/11/2006 4:38 PM:
At a certain point in my code, I need to blank the value in a file
input. The situation comes up when a user first thinks they want to
upload something from their harddrive, but then they change their mind
and decide to choose a file from an online archive. I thought I could
get a reference to the input and then set value to blank, but that only
works in FireFox. For IE, I only got this to work by setting innerHTML
to blank:

if (document.getElementById &&
document.getElementById(idOfFileInputToBeMadeBlank)) {
var referenceToFileInput =
document.getElementById(idOfFileInputToBeMadeBlank);
referenceToFileInput.value = "";
referenceToFileInput.innerHTML = "";
}

However, this will probably break in old versions of IE. So what works
for older versions of IE?

The same thing that will work in any browser that supports form
manipulation:

document.forms['formID'].reset();

Voila! Your file input is blank.
 
J

Jake Barnes

Randy said:
if (document.getElementById &&
document.getElementById(idOfFileInputToBeMadeBlank)) {
var referenceToFileInput =
document.getElementById(idOfFileInputToBeMadeBlank);
referenceToFileInput.value = "";
referenceToFileInput.innerHTML = "";
}

However, this will probably break in old versions of IE. So what works
for older versions of IE?

The same thing that will work in any browser that supports form
manipulation:

document.forms['formID'].reset();

Voila! Your file input is blank.

I appreciate your answer, but it looks like it will blank the whole
form. It's important that the form keep its information, as the user
has input it, and instead we merely blank the one input.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,744
Messages
2,569,481
Members
44,900
Latest member
Nell636132

Latest Threads

Top