admin管理员组文章数量:1425819
I'm new to JS and trying to make an input field that that only appears when the user types anywhere on the page. Currently, the appearing of the input field works but once it's visible, the user still has to click into the field to type in it.
I would like it to work like:
- User presses "S"
- Field appears with "S" typed in it.
Any help would be much appreciated!
var searchArea = $(".search");
searchArea.hide();
$(document).ready(function() {
$( "body" ).keydown(function() {
searchArea.show();
searchArea.elements['input'].focus();
console.log("worked!");
});
});
I'm new to JS and trying to make an input field that that only appears when the user types anywhere on the page. Currently, the appearing of the input field works but once it's visible, the user still has to click into the field to type in it.
I would like it to work like:
- User presses "S"
- Field appears with "S" typed in it.
Any help would be much appreciated!
http://codepen.io/jeremypbeasley/pen/RNrore
var searchArea = $(".search");
searchArea.hide();
$(document).ready(function() {
$( "body" ).keydown(function() {
searchArea.show();
searchArea.elements['input'].focus();
console.log("worked!");
});
});
Share
Improve this question
asked Dec 6, 2014 at 14:13
Jeremy P. BeasleyJeremy P. Beasley
7091 gold badge7 silver badges22 bronze badges
3 Answers
Reset to default 4What is searchArea.elements['input']
supposed to do ?
Try it like this instead, using jQuery's find
method
$( "body" ).on('keydown', function() {
searchArea.show().find('input').focus();
});
PEN
You need to find
the input
element within the seach area, then call focus()
on it:
$(document).ready(function() {
var $searchArea = $(".search").hide();
$("body").keydown(function() {
$searchArea.show();
$searchArea.find('input').focus();
});
});
Updated CodePen
Or just set id to input field (<input autofocus="autofocus" id="hid_inp"
>)
and use $("#hid_inp").focus();
instead of searchArea.elements['input'].focus();"
本文标签: javascriptHow to focus a hidden input fieldStack Overflow
版权声明:本文标题:javascript - How to focus a hidden input field - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1745417793a2657765.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论