Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

jqueryMobile set focus on input field in popup

In my jquery mobile application I would like to automatical set the focus on an input field after a popup is opened. The input field is in the popup as below. The focus is correctly set at the beginning but lost after the popup is fully lowded.

    <!-- Link--><a onclick="$('#popup_input').focus()" href="#popup" data-iconpos="top" data-rel="popup" data-position-to="window" data-role="button" data-inline="false" data-transition="pop" data-icon="plus" data-theme="b">open</a>
<div data-role="popup" id="popup" data-overlay-theme="a" data-theme="c" data-dismissible="false" style="max-width:400px;" class="ui-corner-all">
            <div data-role="header" data-theme="a" class="ui-corner-top">
                 <h1>Popup</h1>

            </div>
            <div data-role="content" data-theme="d" class="ui-corner-bottom ui-content">
                 <h3 class="ui-title">Focused Field</h3>

                <p>
                    <input type="text" id="popup_input" />
                </p> <a href="#" data-role="button"data-rel="back">Close</a>

            </div>
        </div> 

http://jsfiddle.net/kx7Gz/

Thanks for your help

kind reagrds

like image 576
user1584028 Avatar asked Feb 16 '23 16:02

user1584028


1 Answers

Working example: http://jsfiddle.net/Gajotres/BknCc/

You need to use a little bit of javascript to make this work. Don't use inline javascript to set focus on a field. Instead wait for popup to be fully created and show before doing anything.

HTML :

<!DOCTYPE html>
<html>
    <head>
        <title>jQM Complex Demo</title>
        <meta name="viewport" content="width=device-width; initial-scale=1.0; maximum-scale=1.0; minimum-scale=1.0; user-scalable=no; target-densityDpi=device-dpi"/>
        <link rel="stylesheet" href="http://code.jquery.com/mobile/1.3.1/jquery.mobile-1.3.1.min.css" />
        <script src="http://code.jquery.com/mobile/1.3.1/jquery.mobile-1.3.1.min.js"></script>    
    </head>
    <body>
        <div data-role="page" id="index">
            <div data-theme="b" data-role="header">
                <h1>Index page</h1>
            </div>

            <div data-role="content"> 
                <a href="#popup" data-iconpos="top" data-rel="popup" data-position-to="window" data-role="button" data-inline="false" data-transition="pop" data-icon="plus" data-theme="b">open</a>

                <div data-role="popup" id="popup" data-overlay-theme="a" data-theme="c" data-dismissible="false" style="max-width:400px;" class="ui-corner-all">
                    <div data-role="header" data-theme="a" class="ui-corner-top">
                        <h1>Popup</h1>

                    </div>
                    <div data-role="content" data-theme="d" class="ui-corner-bottom ui-content">
                        <h3 class="ui-title">Focused Field</h3>

                        <p>
                            <input type="text" id="popup_input" />
                        </p> <a href="#" data-role="button"data-rel="back">Close</a>

                    </div>
                </div>
            </div>
        </div>    
    </body>
</html>   

Javascript :

$(document).on('pagebeforeshow', '#index', function(){ 
    $( "#popup" ).popup({
        afteropen: function( event, ui ) {
            $('#popup_input').focus();
        }
    });
});    
like image 170
Gajotres Avatar answered Apr 30 '23 07:04

Gajotres