How to re-focus to a text field when focus is lost on a HTML form?

Posted by Horace Ho on Stack Overflow See other posts from Stack Overflow or by Horace Ho
Published on 2010-04-09T08:29:13Z Indexed on 2010/04/09 8:33 UTC
Read the original article Hit count: 368

Filed under:
|
|

There is only one text field on a HTML form. Users input some text, press Enter, submit the form, and the form is reloaded. The main use is barcode reading. I use the following code to set the focus to the text field:

<script language="javascript">
    <!--
            document.getElementById("#{id}").focus()
    //-->
</script>

It works most of the time (if nobody touches the screen/mouse/keyboard).

However, when the user click somewhere outside the field within the browser window (the white empty space), the cursor is gone. One a single field HTML form, how can I prevent the cursor from getting lost? Or, how to re-focus the cursor inside the field after the cursor is lost? thx!

© Stack Overflow or respective owner

Related posts about JavaScript

Related posts about lost-focus