10

I am trying to set focus to the input box and showing android keyboard using jquery mobile on pageshow.

I tried a lot of options from web. but none is working as expected in both emulator and mobile.

Here is the Code:

<!DOCTYPE html>
<html>
    <head>
        <title>Title</title>
        <meta name="viewport" content="width=device-width, initial-scale=1">
        <meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1"/>
        <link rel="stylesheet" href="http://code.jquery.com/mobile/1.2.0/jquery.mobile-1.2.0.min.css" />
    </head>
    <body>
        <div data-role="page" id="main">
            <div data-role="header"><h3>Set Focus, Show Keyboard</h3></div>
            <div data-role="content">
                <label for="gotoPage"></label>
                <input type="text" name="gotoPage" id="gotoPage" placeholder="Question No." data-mini="true"   />
            </div>
        </div>                              

        <script src="http://code.jquery.com/jquery-1.8.2.min.js"></script>
        <script>
            $(document).on('pageinit',"[data-role=page]", function() {  

            });

            $(document).on('pagebeforeshow',"[data-role=page]", function() {                

            });

            $(document).on('pageshow',"[data-role=page]", function() {  
                $('#gotoPage').focus().select();                        
            }); 
        </script>
        <script src="http://code.jquery.com/mobile/1.2.0/jquery.mobile-1.2.0.min.js"></script>      
    </body>
</html>

Find the Screen shots for reference

enter image description here

enter image description here

Kindly advice... Thanks in advance...

NOTE:

As per Omar comment it is working fine in ios.... anyone can suggest how we get work this in android?

10
  • I dont think the select() is needed Commented Jun 4, 2013 at 11:13
  • $(document).on('pageshow',"[data-role=page]", function() { $('#gotoPage').trigger('click') }); you may need to set some delay using setTimeout. Commented Jun 4, 2013 at 11:18
  • can u please provide the exact code... Commented Jun 4, 2013 at 11:23
  • 1
    $(document).on('pageshow', "[data-role=page]", function () { setTimeout(function () { $('#gotoPage').trigger('click'); }, 50); }); forgot ; after .trigger. Commented Jun 4, 2013 at 11:25
  • Sorry..i tried this, but its not working.. Commented Jun 4, 2013 at 11:29

1 Answer 1

3

The solution which works for me:

$("#main").on("pageshow" , function() {
$('#gotoPage').focus();
});
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.