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
2 Answers
Reset to default 5Can'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
版权声明:本文标题:Javascript focus on browse button of file input - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1741286660a2370309.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论