By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
435,319 Members | 1,963 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 435,319 IT Pros & Developers. It's quick & easy.

Blank out a file field

P: n/a
Hi all,
I have the following codes,

<script language="JavaScript">
function check_stuff(field)
{
alert("blank out the field now");
field.value = "";
}
</script>

<form name="test_form">
<input type="File" name="test_upload" size="50">
<input type="Button"
onclick="check_stuff(document.test_form.test_uploa d)" value="Click Me">
</form>

What I am trying to do is, when the users click the button, it would
blank out the path in the test_upload field. The problem is, the
test_upload field does not get cleared out and it is not giving me an
error either. May I ask how can I clear out the file field?

Thanks in advance,
Monte
*** Sent via Developersdex http://www.developersdex.com ***
Jul 23 '05 #1
Share this Question
Share on Google+
1 Reply


P: n/a
Monte Chan wrote:
Hi all,
I have the following codes,

<script language="JavaScript">
function check_stuff(field)
{
alert("blank out the field now");
field.value = "";
}
</script>

<form name="test_form">
<input type="File" name="test_upload" size="50">
<input type="Button"
onclick="check_stuff(document.test_form.test_uploa d)" value="Click Me">
</form>

What I am trying to do is, when the users click the button, it would
blank out the path in the test_upload field. The problem is, the
test_upload field does not get cleared out and it is not giving me an
error either. May I ask how can I clear out the file field?


Javascript security prevents you from changing the value attribute of a
file input. If you knew (or guessed) the location of a file, you could
simply change the value of the input onsubmit and get the user to send
you any file you liked rather than the one they'd selected.

You can replace the input with a new one, which will have the same
effect as 'blanking' the current one. This is not optimal as you must
copy over the properties of the old input to the new one - if you clone
the node the new one will have the same unalterable file value as the
old one.

Below is a script that creates a new input, changes it's type, matches
the id and name of the old input (if it has them), then replaces the
old with the new. You may want to copy over other attributes too, say
className, style properties, etc.

<form action="">
<input type="file" name="fifo">
<input type="submit" value="Send file"><br>
<input type="button" value="Clear filename" style="width: 10.7em;"
onclick="
var z = this.form.fifo;
var x = document.createElement('input');
x.type = 'file';
if (z.name) x.name = z.name;
if (z.id) x.id = z.id;
this.form.replaceChild(x,z);
">
</form>


--
Rob
Jul 23 '05 #2

This discussion thread is closed

Replies have been disabled for this discussion.