Search code examples
jqueryjquery-mobiletextboxcursorfocus

How to set the cursor in text box on page init


I am doing a mobile application in jquery mobile. When the first page is loading it have an init function. My requirement is that when the first page is loaded the cursor should point to the text box. I tried:

  $('#txtdemo').focus();

and

   var txtBox=document.getElementById("txtdemo");
   txtBox.focus();

but both are not working. Please let me know a good approach to do this.


Solution

  • In mobile site, it is impossible to focus in textbox and open keyboard using programming. it is one type of usability issue. In desktop site you can do this with

    $('#txtdemo').focus();
    
    $('#txtdemo').select();
    
    $('#txtdemo').trigger('focus');
    
    $('#txtdemo').trigger('click');