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 Answer 1

4
 $("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."

Sign up to request clarification or add additional context in comments.

3 Comments

This is great for Chrome, but it doesn't work on Firefox... :-(
Take a look at the link below, there does not seem to be a direct solution, need to find a workaround. bytes.com/topic/javascript/answers/…
A few years later: works on all major current browsers: Chrome 28, Firefox 22, IE 10, Safari 6 (MacOSX)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.