2

How can I push a file to an input (file type) element using the files property ?

element2.files.push(element.files[0]);

The code above give me an error:

TypeError: element2.files.push is not a function

The source can be datatransfer from drag and drop or another input.

user3541631
  • 3,686
  • 8
  • 48
  • 115
  • I'm not sure that the question makes sense. I don't think you can set the input. If you want to be able to drag and drop files into the input you handle the `drop` event, like [this](https://developer.mozilla.org/en-US/docs/Using_files_from_web_applications#Selecting_files_using_drag_and_drop). For your other use case, I don't know if it's possible to set an input to the value of another one programmatically though. – Karl Reid Oct 04 '17 at 10:42
  • See this link:https://stackoverflow.com/questions/1696877/how-to-set-a-value-to-a-file-input-in-html – Kannan K Oct 04 '17 at 10:44

1 Answers1

0

It is because element2.files is not an Array . So you cannot use push function for add a item into a object as element2.files is a object . You can check it using :

element2.files instanceof Array 
element2.files instanceof Object
aks7
  • 431
  • 3
  • 12