Saturday, June 1, 2013

JQuery Mobile - Dynamically build listview (ul li)

http://jsfiddle.net/SuSpv/

<!DOCTYPE html>
<html>
    <head>
        <meta name="viewport" content="width=device-width, initial-scale=1">
        <link rel="stylesheet" href="http://code.jquery.com/mobile/1.0/jquery.mobile-1.0.min.css" />
        <script type="text/javascript" src="http://code.jquery.com/jquery-1.6.4.min.js"></script>
        <script type="text/javascript" src="http://code.jquery.com/mobile/1.0/jquery.mobile-1.0.min.js"></script>
        <script>
       
            var listCreated = false;
           
            function appendToList(){
               
                //Create the listview if not created
                if(!listCreated){
                    $("#content").append("<ul id='list' data-role='listview' data-inset='true'></ul>");
                    listCreated = true;
                    $("#content").trigger("create");
                }
                $("#list").append("<li>Item</li>");
                $("#list").listview("refresh");
               
            }
        </script>
    </head>
    <body>

        <div data-role="page">

            <div data-role="content" id="content">
                <input type="button" value="Add item to list" onclick="appendToList()"/>
            </div><!-- /content -->
        </div><!-- /page -->
    </body>
</html>

0 comments:

Post a Comment