I have a search bar, and when I open the search bar I want to start typing right away and do not want to click on the text box for that to happen.
I tried to use myTextBoxId.click() ( because when you click it, you can start typing ) but it didn't work.
I have a search bar, and when I open the search bar I want to start typing right away and do not want to click on the text box for that to happen.
I tried to use myTextBoxId.click() ( because when you click it, you can start typing ) but it didn't work.
javascript
jquery
textbox
Share
Improve this question
edited Oct 10, 2016 at 17:50
Vaibhav Mule
5,14644 gold badges3737 silver badges5252 bronze badges
asked Oct 10, 2016 at 16:59
Hen EliezerHen Eliezer2133 bronze badges1
1Use myTextBoxId.focus() instead.
– Mohammad
CommentedOct 10, 2016 at 17:01
Add a ment
|
6 Answers
6
Reset to default
3
You have to
document.getElementById(" <put your Id here> ").focus();
to set focus on the given element.
You can set the focus once you load the page, or trigger the focus on keydown (this way even if you set the focus out of the input - when you start typing the focus will get there again).
You can use myTextBox.focus() to achieve this. Note that you can call focus() after myTextBox variable is set to point to your actual text box. See: HTMLElement focus