admin管理员组

文章数量:1279053

I am trying to focus on the browse button of the file input control.

so I have something like

 <input type="file" name="upload" id="upload" >

and in javascript I have

document.getElementById("upload").focus();

but the focus remain on the text field and es to browse button only after i hit tab. Is there a way that I could write a script to set the focus on the browse button?

Thanks for your help!

I am trying to focus on the browse button of the file input control.

so I have something like

 <input type="file" name="upload" id="upload" >

and in javascript I have

document.getElementById("upload").focus();

but the focus remain on the text field and es to browse button only after i hit tab. Is there a way that I could write a script to set the focus on the browse button?

Thanks for your help!

Share Improve this question edited May 8, 2012 at 11:44 alain.janinm 20.1k11 gold badges67 silver badges113 bronze badges asked Dec 18, 2009 at 1:52 zoom.pat27zoom.pat27 311 gold badge1 silver badge2 bronze badges 1
  • could you show a little more of code? The javascript seems to be Ok, but it depends when it is called if it will work. When do you want to call that javascript to change the focus (on page load, after writing 10 chars in the textbox...)? – Edgar Hernandez Commented Dec 18, 2009 at 2:09
Add a ment  | 

2 Answers 2

Reset to default 5

Can't be done. You have almost zero control over the constituent parts of a file upload field, partly for security reasons and partly because the standards do not define what constituent parts a file upload field might have. It is entirely possible the browser might render a file upload interface without any ‘Browse’ button.

This question's been around for a while, allowing standards to evolve and develop with new functionality, but I've had success with the following code:

<input type="file" id="file_field" />

<input type="button" value="click me"
    onclick="document.getElementById('file_field').click()" />

I've managed to test it in Chrome and Internet Explorer 7, 8 and 9 so far, and I suspect it would work in Firefox too. Hope this helps someone!

本文标签: Javascript focus on browse button of file inputStack Overflow