Eric Willigers
Eric Willigers

Reputation: 1806

Setting file upload contents using script

Is there a way to use JavaScript to set the file name and contents of a form's file uploads?

For example, suppose we have two file input elements.

<input type="file">
<input type="file">

Can JavaScript copy the content from one form element to the other?

Upvotes: 3

Views: 45

Answers (1)

Muhammad Usman
Muhammad Usman

Reputation: 10148

Add an onchange event on any of the input type file

Get its file property and assign to other input like

element2.files = element1.files;

var el = document.getElementById("1");
var el1 = document.getElementById("2");
el.onchange = function(){
el1.files = el.files;
 

}
<input type="file" id="1">
<input type="file" id="2">

Upvotes: 1

Related Questions