Page 1 of 1

file field value

Posted: Tue May 11, 2010 11:15 pm
by m2babaey
Hi
I use this code in my ajax action file to clear form field after submit:

Code: Select all

<script>
parent.document.getElementById('field_id').value='';
</script>
It works for input fields but doesn't work for file fields in IE
Any help would be extremely appreciated
Thanks in advance

Re: file field value

Posted: Wed May 12, 2010 12:52 am
by Christopher
You might want to try element.setAttribute("value", "").

Re: file field value

Posted: Wed May 12, 2010 1:00 am
by m2babaey
Thanks
But I have not used this method before
Should I replace:

Code: Select all

<script>
parent.document.getElementById('field_id').value='';
</script>
with

Code: Select all

<script>
field_id.setAttribute("value", "").
</script>
?

Re: file field value

Posted: Wed May 12, 2010 7:10 am
by John Cartwright
If you are accessing the parent's DOM, then

[text]parent.document.getElementById('field_id').setAttribute('value', '');[/text]

Re: file field value

Posted: Thu May 13, 2010 12:54 pm
by ell0bo
You can't force the value of a file input type, doing so would be a security issue.

Think of it this way, I create a page that has a hidden form. I keep rotating the value of that input field to look for files I might want to steal. I then auto submit the page to an iframe. I would be able to just steal files off of anyone's computer.

Re: file field value

Posted: Thu May 13, 2010 5:12 pm
by John Cartwright
ell0bo wrote:You can't force the value of a file input type, doing so would be a security issue.

Think of it this way, I create a page that has a hidden form. I keep rotating the value of that input field to look for files I might want to steal. I then auto submit the page to an iframe. I would be able to just steal files off of anyone's computer.
Excellent point.

After discussing this with a colleague, evidently a good way to manipulate the file input to it's original blank value is to re-insert the file input back into the DOM, thus overwriting the current input element. I.e.,

Code: Select all

<script>
    function clearFileInput(elementId)
    {
        document.getElementById(elementId).innerHTML = document.getElementById(elementId).innerHTML;
    }
</script>

<div id="fileInputContainer">
    <input type="file" name="myFileUpload" /> 
</div> 

<a href="#" onclick="clearFileInput('fileInputContainer');">Clear File Input</a>
Alternatively, you can use JS to change the input type of the HTML, then change it back to a file input. This will clear the currently set value. I.e.,

Code: Select all

<script>   
    function clearFileInput(elementId) {
        document.getElementById(elementId).setAttribute('type', 'input');
        document.getElementById(elementId).setAttribute('type', 'file'); 
    }
</script>


<input type="file" name="myFileUpload" id="fileInput" /> 

<a href="#" onclick="clearFileInput('fileInput');">Clear File Input</a>