Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

copying the value of a form's file input field to another form's input field

So I have two forms, both have a file type input field and I tried

$('.inputfield1').change(function(){
   var file = $(this).val();
   $('.inputfield2').val(file);
});

but then it doesn't get copied properly and firebug complains about "Security Error" in the error console

what did I do wrong and how can I properly copy the value of a file input field

by the way, the destination form has a target that is set to an iframe (not a different domain)

like image 432
pillarOfLight Avatar asked Jan 17 '12 16:01

pillarOfLight


People also ask

How do I copy values from one input field to another?

Simply register an input even handler with the source textfield and copy the value to the target textfield .

How do I copy a field in another form?

Simply make a selection of fields and press the 'Copy to form' button at the top of the page. You'll be prompted to selected the destination form you'd like to copy the selected fields to. Make your selection and you're done!

How do you assign a value to input field?

Sometimes we need to set a default value of the <input> element, This example explains methods to do so. This property set/return the value of value attribute of a text field. The value property contains the default value, the value a user types or a value set by a script.


3 Answers

You can't move the value of one file input to another. Instead, clone the input, place the clone where the original is, and move the original into the hidden form.

$(".inputfield1").change(function(){   var $this = $(this), $clone = $this.clone();   $this.after($clone).appendTo(hiddenform); }); 
like image 196
Kevin B Avatar answered Sep 21 '22 23:09

Kevin B


I know it is a late answer, but I had a similar problem that I just figured out today.

What I did was move the File input to the new location after deleting the current one in the other location. By moving the element, everything stayed intact on all my tests.

$('.inputfield1').change(function() {   $('.otherinputfield').remove();   $('#newform').append($(this)); }); 
like image 32
Adrian Stride Avatar answered Sep 21 '22 23:09

Adrian Stride


If you need to copy the file from one input to another, you could use the below method.

$(".inputfield2")[0].files = $(".inputfield1")[0].files;
like image 40
Ameer Avatar answered Sep 19 '22 23:09

Ameer