Passing data from dynamic listview
Hi there,
I'm having trouble with some beginner case:
There is a button which appends a li element with id ="123" to an ul element (listview) ... then i wanna click on that new element and a new page should appear saying "element id = 123".
It kind of works as long as the li elements were not appended dynamicly but hard coded in the html part.
Whats my fault here?
- <script>
- var passDataObject = { selectedId: null }
-
- $(document).ready(function(){
- $("#acclist a").on("click", function(e){
- e.preventDefault();
- passDataObject.selectedId = $(this).closest("li").attr("id");
- $.mobile.changePage('#accountdetail');
- });
-
- $("#btn").click(function(){
- var output = '<li id="123"><a href="#">Test Account</a></li>';
- $("#acclist").append(output).listview('refresh');
- });
- });
- $(document).on( "pagebeforeshow", "#accountdetail", function( e ) {
- $("#details").html(["Selected id is: '", passDataObject.selectedId, "'"].join(""));
- });
- </script>
- </head>
- <body>
- <div data-role="page" id="accountpage">
- <div data-role="content">
- <button id="btn">Add</button>
- <br/>
- <ul id="acclist" data-role="listview">
- <!--<li id="123"><a href="#">test</a></li>-->
- </ul>
- </div>
- </div>
-
- <div data-role="page" id="accountdetail">
- <div data-role="content">
- <div id="details"></div>
- </div>
- </div>
- </body>