Jump to content

DOM defaultValue property for file inputs


John McKean

Recommended Posts

Newbie - please forgive my ignorance.I have an upload form and I want to put a default value in the filename input.Since file inputs have no associated value="xx" method, I tried the following JavaScript using the DOM defaultValue property. I added a text input (test1) to verify that the script is executed. IE-6 correctly puts "abc" in the test1 box, but doesn't modify the file box or pass the defaultValue ("c:\test1.txt") to my PHP CGI file.Mozilla 1.7.8 doesn't appear to do anything at all.Is there something wrong with my approach or another method that would work?Thanks,John

<html><head>  <meta content="text/html; charset=ISO-8859-1" http-equiv="content-type">  <meta http-equiv="PRAGMA" content="NO-CACHE"><title>Upload</title><script type="text/javascript">function setDefaults(){document.getElementById("test1").value="abc"document.getElementById("userfile").defaultValue="c:\test1.txt"}</script></head><body onload="setDefaults()"><form enctype="multipart/form-data" action="test_upload.php" method="post"><input type="hidden" name="MAX_FILE_SIZE" value="1000000"><input name="test1" type="text">File to send: <input name="userfile" type="file" size="50"><br><br><input type="submit" value="Send File"></form></body></html>

Link to comment
Share on other sites

  • 2 years later...
Guest FirefoxRocks

Instead of using the name attribute, use the id attribute like this:

<input name="userfile" id="userfile" type="file" size="50">

As far as I know, IE incorrectly selects the element even with the name attribute when using document.getElementById.

Link to comment
Share on other sites

I would be very surprised if a modern browser let you set a default path for a file element. Since the file structure of every common OS is well-known, this ability would let web authors troll for files on the hard drive of any user that came along, stealing your passwords, etc. Very bad security.

Link to comment
Share on other sites

FirefoxRocks, please stop digging up two-and-a-half years old topics :)

Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
×
×
  • Create New...