Search code examples
jqueryjquery-uijquery-mobilejquery-mobile-listviewjquery-mobile-popup

dynamically creating jquery mobile list view within a jquery mobile popup


This has been driving me insane so hopefully someone can help me out :-D

I am trying to create a jquery mobile popup and within the popup I want to dynamically create a jQuery mobile listview. However I keep getting the following error message.

Uncaught TypeError: Cannot read property 'jQuery19104145257784985006' of undefined.

Here is my code

$('#create').on('click', function () {
    //create a div for the popup
    var $popUp = $("<div/>").popup({
        dismissible: false,
        theme: "a",
        overlyaTheme: "a",
        transition: "pop"
    }).on("popupafterclose", function () {
        //remove the popup when closing
        $(this).remove();
    });

    //create a title for the popup
    $("<ul data-role='listview'/>").trigger("create").appendTo($popUp);

    $popUp.popup('open').trigger("create");
});

I have created a basic jsFiddle here http://jsfiddle.net/QA7Dm/

Any help is gratefully appreciated.


Solution

  • Working example: http://jsfiddle.net/Gajotres/Ar8N3/

    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.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>   
    

    JS :

    $(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);      
        });    
    });