The click function works for the <li>
element that is part of the HTML but not for the <li>
elements loaded programmatically on pageinit. I can't figure out why not. (this code has all it needs to run)
<!DOCTYPE html>
<html>
<head>
<meta name="viewport" content="width=device-width, initial-scale=1">
<link rel="stylesheet" href="http://code.jquery.com/mobile/latest/jquery.mobile.css" />
<script src="http://code.jquery.com/jquery.js"></script>
<script src="http://code.jquery.com/mobile/latest/jquery.mobile.js"></script>
</head>
<body>
<div id="thelists" data-role="page">
<div data-role="header">
<h1>My Title</h1>
</div><!-- /header -->
<div data-role="content">
<p>Hello world</p>
<ul id="allyourlists" class="current" data-role="listview" data-filter="false">
<li><a href="index.html" data-role="button" id="delalist">List0:</a></li>
</ul>
</div><!-- /content -->
</div><!-- /page -->
<script>
//Why is delete button not firing?
$('#thelists').bind('pageinit', function(event) {
console.log('in bind pageinit for yourlists');
var thelists = ["list1", "list2"];
console.log(thelists);
$.each(thelists, function(index, alist) {
$('#allyourlists').append('<li><a href="index.html" data-role="button" id="delalist">List: ' + alist + '</a></li>');
});
$('#allyourlists').listview('refresh');
});
//gets the val of val1 from the previois call
$("#delalist").click(function (e) {
e.stopImmediatePropagation();
e.preventDefault();
alert ('in delalist') ;
});
</script>
</body>
</html>