When I add content to my listview in jQuery Mobile, they don’t trigger the click event when clicked. In my example below, ‘Cadillac’ is not clickable. Am I doing something wrong?
HTML:
<div data-role="page" id="p1">
<ul data-role="listview" data-theme="b">
<li><a href="#">Acura</a></li>
<li><a href="#">Audi</a></li>
<li><a href="#">BMW</a></li>
</ul>
</div>
jQuery:
$('li').on('click',function(){
alert('clicked');
});
$('ul').append('<li><a href="#">Cadillac</a></li>');
$('ul').listview('refresh');
Try to change your code to this
EDIT: In this case
onfunction automagically bindsclickto everylielement in list even when DOM changes. So you can add list items whenever you want. All of them will have click event binding.Using old
livejQuery method with almost same functionality is a bad idea cause of it very sloooow performance.