jQM - Dynamically create popup

by Dragan Gaić

HTML

<link rel="stylesheet" href="http://jeromeetienne.github.com/jquery-mobile-960/css/jquery-mobile-fluid960.css">
<script src="http://timeago.yarp.com/jquery.timeago.js"></script>
<script src="http://imakewebthings.github.com/jquery-waypoints/waypoints.min.js"></script>
<!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.2.0/jquery.mobile-1.2.0.min.css" />
        <script src="http://code.jquery.com/mobile/1.2.0/jquery.mobile-1.2.0.min.js"></script>    
    </head>
    <body>
        <div data-role="page" id="index">
            <div data-theme="a" data-role="header">
                <h3>
                    First Page
                </h3>
                <a href="#second" class="ui-btn-right">Next</a>
            </div>
            
            <div data-role="content">
                <a href="#" data-role="button" id="create">Create a popup</a>
            </div>
            
            <div data-theme="a" data-role="footer" data-position="fixed">
                
            </div>
        </div>     
    </body>
</html>

JavaScript

$(document).on('pageshow', '#index', function(){ 
    $(document).on('click', '#create', function(){     
        $('<div>').attr({'data-role':'popup','id':'popupBasic','data-dismissible':'false','data-theme':'a','data-transition':'pop'}).appendTo('[data-role="content"]');
        $('<div>').attr({'data-role':'header','data-theme':'b','id':'popup-header'}).append('<h1>Header</h1>').appendTo('#popupBasic');
        $('<ul>').attr({'data-role':'listview','id':'list-test','data-theme':'a'}).appendTo('#popupBasic');
        $('<li>').append('List test').appendTo('#list-test');
        $('#index').trigger('pagecreate');
        var popup = setInterval(function(){
            $("#popupBasic").popup("open",{
                overlyaTheme: "a"
            }).on("popupafterclose", function () {
                //remove the popup when closing
                $(this).remove();
            });
            clearInterval(popup);
        },1);      
    });    
});