views:

1213

answers:

5

I would like to copy the value of a input:file to input:text. I can do it with plan JavaScript but I will like to know how to do it with jQuery.

----JavaScript

// This what the script would look like with plain JavaScript
// It works fine. I just woulld like to know who to do it with jQuery.
function fakeScript() { 
var filepath; 
filepath = document.adminForm.tumb.value; 
filepath = filepath.substring(filepath.lastIndexOf('\\')+1, filepath.length); 
document.adminForm.tumbFake.value = filepath; 
}
A: 

Sorry, can't be done can't be done with reliable results for security reasons neither with Javascript nor with JQuery at least in Internet Explorer > 8.

Internet Explorer 8 and later. When a file is selected by using the input type=file object, the value of the value property depends on the value of the "Include local directory path when uploading files to a server" security setting for the security zone used to display the Web page containing the input object.

Source

Your only chance is fiddling with a Java or flash based uploader like SWFUploader

Pekka
File inputs are read only. Since the OP isn't trying to write to one, this is not the problem.
David Dorward
Not entirely true (although probably not the OP's problem). Check my edited answer.
Pekka
+2  A: 

If you have something that works in "plain Javascript", it'll work with jQuery too : jQUery is just a library, that adds functions -- it doesn't prevent anything from working (or there is some kind of bug in it ^^ )

Pascal MARTIN
A: 

To get the text value of a file from an <input type="file"/> element, use yourFileInputElement.files[0].getAsText("utf-8").

Eli Grey
+1  A: 
var fileValue=$("input[type='file']").val();
var inputValue=$("input[type='text']").val(fileValue);

cheers

Marko
A: 

You probably cannot use the value attribute of a file input with JQuery.

"The value attribute cannot be used with <input type="file">." - http://www.w3schools.com/tags/att_input_value.asp

Bryan