Question

I'm trying to set focus on a textbox in jQuery mobile. But it is not working. this is my code

        <script type="text/javascript">
            function FocusOnInput()
            {
                document.getElementById("username").focus();
            }
        </script>

and this is my text box.

<input name="username" id="textinput5"  type="text">
Was it helpful?

Solution

You're using document.getElementById("username") when the ID is actually textinput5.

document.getElementById("username").focus(); should be document.getElementById("textinput5").focus();

OTHER TIPS

You could also try this code. The function is called when the page is loaded and is made visible:

<script>
     $(document).ready(function () {
       $("#input:text:visible:first").focus();         
     });
</script>

Instead of :

function FocusOnInput()
{
  document.getElementById("username").focus();
}
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top