Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

HTML File input JS events

Tags:

Are there any JavaScript events in type=file input? I.E. I would like to add an extra file input upon selecting file in one of allready created:

Diagram:] :

file input 1 file input 2 file input 3 

user selects some file in input 1 and JS adds new file input

file input 1 - somefile.txt file input 2 file input 3 *NEW* file input 4  

I'm looking for meaningful event, on which i can bind addFileInput method.

I also use jQuery 1.4 :]

Thank you

like image 341
Adam Kiss Avatar asked Feb 19 '10 13:02

Adam Kiss


People also ask

How do you assign a value to a file input?

The only way to set the value of a file input is by the user to select a file. This is done for security reasons. Otherwise you would be able to create a JavaScript that automatically uploads a specific file from the client's computer.

What is input event in HTML?

The input event fires when the value of an <input> , <select> , or <textarea> element has been changed. The event also applies to elements with contenteditable enabled, and to any element when designMode is turned on. In the case of contenteditable and designMode , the event target is the editing host.

How do you attach a file in HTML?

The <input type="file"> defines a file-select field and a "Browse" button for file uploads. To define a file-select field that allows multiple files to be selected, add the multiple attribute. Tip: Always add the <label> tag for best accessibility practices!

How do you append files in input type file multiple before uploading?

You could add a new <input type="file"> whenever you finished uploading the previous files and hide the previous input. This way you keep adding a new input every time you want to add more files and prevent the previous input from being overwritten.


2 Answers

I believe onchange should work.

like image 148
Ionuț G. Stan Avatar answered Sep 17 '22 02:09

Ionuț G. Stan


Here is the event logged by Firebug when selecting a file with Firebug:

  • click clientX=885, clientY=207
  • blur
  • focus
  • change
  • DOMActivate
  • DOMActivate
  • mouseout clientX=162, clientY=27

I guess change, is the one you are looking for:

$ ('#your_form_id input[type=file]').live ('change', function () {   $(this).parent ().append ($('<input type="file" />')); }) 

you just have to adapt the selector and the previous code should work

like image 25
Mathieu Avatar answered Sep 21 '22 02:09

Mathieu