How to focus an input field on Android browser through javascript or jquery

16,230

Solution 1

Actually, the general javascript function "focus" is deactivated in the android browser. Hence, the jQuery focus function is deactivated since it's using the above.

Solution 2

if you bind it to another click event it will work. This works for me:

    $(document).ready(function()
    {

       $('#field').click(function(e){ $(this).focus(); });

            $('body').click(function(e)
            {
                $('#field').trigger('click');
            })
    })   

Will pop up the software keyboard. trigger() will trigger any event you give it. In this case the default behaviour of clicking on the field == tap == focus == win! Note: this call is bound to another click event happening.

Share:
16,230
Ionut Bilica
Author by

Ionut Bilica

Updated on July 05, 2022

Comments

  • Ionut Bilica
    Ionut Bilica almost 2 years

    I've tried $('#field').focus(), and any other method found on the internet. Nothing worked. I have a simple html that reproduces the problem.

    <!DOCTYPE html> 
    <html> 
        <head> 
            <script type="text/javascript" src="https://ajax.googleapis.com/ajax/libs/jquery/1/jquery.min.js"></script>
            <script type="text/javascript">
                $(document).ready(function() {
                    $('#field').focus();
                });
        </script>
    </head> 
    <body>
        <input type="text" id="field" name="field"/>
    </body>
    </html>
    

    Please help!

  • ʇsәɹoɈ
    ʇsәɹoɈ almost 12 years
    Can you link a source of more information on this, or a workaround?