Results 1 to 9 of 9

Thread: How to clear <INPUT TYPE="FILE">

  1. #1
    Join Date
    Aug 2007
    Posts
    3
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default How to clear <INPUT TYPE="FILE">

    Hi,

    Is there a way to clear the value of for <INPUT TYPE="FILE" id="File2"> using javascript?

    When user click on button, I need to clear the value of File2

    <input type="button" name="c" value="Test" onclick="clearFile()">

    function clearFile() {

    document.a.File2.value = "";
    }

    is not working?

    Thanks in advance!

  2. #2
    Join Date
    Mar 2005
    Location
    SE PA USA
    Posts
    30,495
    Thanks
    82
    Thanked 3,449 Times in 3,410 Posts
    Blog Entries
    12

    Default

    You are not allowed to do that because, if you were, you could also change the filename and path of the file that the user is sending from his or her computer. That would enable you to spy on the user. I understand that you only want to reset the field. One solution that I have seen put forth is to have the file input element as the only element in a separate form. That way you can supposedly clear it by resetting that form.
    - John
    ________________________

    Show Additional Thanks: International Rescue Committee - Donate or: The Ocean Conservancy - Donate or: PayPal - Donate

  3. #3
    Join Date
    Aug 2007
    Posts
    3
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default

    Thanks John!

    Is it possible to reset the form in a a javascript function & then still submit the form?

    e.g.
    document.a.reset();
    document.a.submit();

    I just tried that; It's not working for me.Thats what I need to do.

    I want to make sure when user click on a button to submit the form, the file field must be blank

  4. #4
    Join Date
    Mar 2005
    Location
    SE PA USA
    Posts
    30,495
    Thanks
    82
    Thanked 3,449 Times in 3,410 Posts
    Blog Entries
    12

    Default

    Why? If you reset 'a' and then submit 'a', 'a' will have no values to be submitted.
    - John
    ________________________

    Show Additional Thanks: International Rescue Committee - Donate or: The Ocean Conservancy - Donate or: PayPal - Donate

  5. #5
    Join Date
    Aug 2007
    Posts
    3
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default

    my mistake; it worked.

    Thanks again.

  6. #6
    Join Date
    Dec 2008
    Posts
    2
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Thumbs up Try this

    HTML Code:
    <script language="javascript">
    
    function fnReset()
    {
    alert("ehere");
    window.form1.reset();
    
    }
    
    </script>
    </head>
    
    <body>
    <form title="form1" id="form1">
    <input  type="file" name="fileob">
    <input  type="reset" name="reset" onCLick="fnReset()">
    
    </form>

    Im sure this works on FF and IE

    Cheers
    Sumit
    Last edited by Snookerman; 04-23-2009 at 04:29 PM. Reason: added [html] tags

  7. #7
    Join Date
    Dec 2008
    Posts
    2
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default

    This code will work. Safari has issue with this code.

  8. #8
    Join Date
    Apr 2009
    Posts
    1
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default

    Solution with one line javascript function here:
    http://gusiev.com/?p=11

  9. #9
    Join Date
    Mar 2005
    Location
    SE PA USA
    Posts
    30,495
    Thanks
    82
    Thanked 3,449 Times in 3,410 Posts
    Blog Entries
    12

    Default

    Quote Originally Posted by agresso View Post
    Solution with one line javascript function here:
    http://gusiev.com/?p=11
    That's a rather innovative approach, but has some potential problems. For the time being though, I suppose it is acceptable. I would avoid the:

    Code:
    href="javascript:whatever()"
    construct unless your onclick event returns false, as this can cause problems in IE. Using innerHTML is non-standard, but effective here. It may not be future compatible. Unfortunately the only standards compliant method would be to replace the file input with a similar DOM element. Unfortunate in that it would require duplicating any unique characteristics of the file input without cloning (which will also clone any value it has) directly - a more individualized approach than the simplicity of innerHTML (which loses the value, a bug that becomes a feature here).
    - John
    ________________________

    Show Additional Thanks: International Rescue Committee - Donate or: The Ocean Conservancy - Donate or: PayPal - Donate

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •