How to set the focus on a hidden textbox field using JavaScript?

focus, javascript

Solution

If you really need to do this, make the box transparent, not hidden:

opacity:0;
filter:alpha(opacity=0);

Alternatively, if you want to ensure that the user doesn't accidentally click it, just place the input inside a div with

width: 0;
overflow: hidden;

However, there is most certainly a better way to do what you want, maybe using `keydown`/`keypress` events.

Problem

How can I set the focus on a hidden textbox element? I tried this: ``` document.getElementById("textControl_fd_component_JSON_TASK_NStext64").focus(); ``` But, this does not work here. As `alert(document.activeElement.id);` returns `null` in this case. If I make this field visible, the above script works fine. Any ideas where I am getting it wrong?

Original source