tags:

views:

59

answers:

2

I'm trying to assign the value of <input type="file" id="field1"> to <input type="file" id="field2">.

I'm having the following code but it is not working as expected:

<script type="text/javascript"> 
function test(){
   var field_value = document.getElementById('file1').value;
   document.getElementById('file2').value = field_value;
}
</script>
<body>
  <input type="file" onchange="test()" id="file1"/>
  <input type="file" id="file2"/>
</body>

Explanation: When I click on the browse field of file field having id="file1" it calls test() function. Variable field_value is having the name of the file uploaded. But it is not assigning the value to file field having id="file2".

+1  A: 

You cannot get nor set the value of an <input type="file" > in Javascript.

remi
Actually you *can* get the value, but it's not really the complete path - the only useful part is the "name" part (the tail end of the path). You're right that it cannot be set, however.
Pointy
But if I am getting the value I might be able to set it.
+2  A: 

For security reasons you cannot access the file controls with Javascript (imagine if you could automatically upload any file you want when the user visits a page!)

Having said that, the newest version of Firefox has a new File API, which might help you for that specific browser. And there are various File controls introduced in HTML5, but it will be a long time before you can use them.

DisgruntledGoat