Passing data from dynamic listview

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?

  1. <script>
  2.             var passDataObject = { selectedId: null }
  3. $(document).ready(function(){
  4. $("#acclist a").on("click", function(e){
  5. e.preventDefault();
  6. passDataObject.selectedId = $(this).closest("li").attr("id");
  7. $.mobile.changePage('#accountdetail');
  8. });
  9. $("#btn").click(function(){
  10. var output = '<li id="123"><a href="#">Test Account</a></li>';
  11.                     $("#acclist").append(output).listview('refresh');
  12. });
  13. });

  14.             $(document).on( "pagebeforeshow", "#accountdetail", function( e ) {
  15.                 $("#details").html(["Selected id is: '", passDataObject.selectedId, "'"].join(""));
  16.             });
  17.         </script>
  18.     </head>
  19.     <body>
  20.         <div data-role="page" id="accountpage">
  21. <div data-role="content">
  22. <button id="btn">Add</button>
  23. <br/>
  24. <ul id="acclist" data-role="listview">
  25.                                     <!--<li id="123"><a href="#">test</a></li>-->
  26.                                 </ul>                       
  27. </div>
  28. </div>
  29.     
  30. <div data-role="page" id="accountdetail">
  31. <div data-role="content">
  32. <div id="details"></div>
  33. </div>
  34. </div>
  35.     </body>