views:

31

answers:

1

I have a form in my HTML with field.

I'd like to show the file selection window when the user clicks somewhere else (for example, an image on other part of the screen). The reason I want to do this is that I have a form with many fields, posting to some action "x", and the file selection form on other part of screen, submiting to action "y"

<form action="x">
   <input type="text" name="field1">
   <input type="text" name="field2">
   <input type="text" name="field3">
   <img src="select_file.png" onclick="//triggers file selection on other form">
   <input type="text" name="field4">
   <input type="text" name="field5">
</form>

<form action="y">
  <input type="file" name="myfile">
</form>

Is there any other way to do this?

+1  A: 
 $("img").click(function(){
    $("input[name=myfile]").trigger('click');
 });

Demo. Click on the envelope image.

Edit: Giving ID's on the each of input fields will make the code more readable and "good."

Kshitij Parajuli
This is great for Chrome, but it doesn't work on Firefox... :-(
Daniel Cukier
Take a look at the link below, there does not seem to be a direct solution, need to find a workaround. http://bytes.com/topic/javascript/answers/542877-programmatically-invoke-browse-dialog-input-type-file
Kshitij Parajuli