I was having some troubles when trying to set the on click listerner for <li>
dynamically. Here is my code to populate the list:
for(var i = 0; i < chatlist.length; i++){
$('<li class="contact"><div class="wrap"><div class="meta"><p class="name">' + contact + '</p><p class="preview">' + preview + '</p></div></div></li>').appendTo($('#contacts ul'));
}
On click listener for selected row:
$('#contacts').on('click', 'li', function() {
var index = $(this).index();
console.log(index);
});
I am using this template. Upon selecting row, I wanted to set the selected <li>
tag to
<li class = 'contact active'></li>
I managed to get the selected row index but I not sure how to set the HTML class for selected <li>
. Any idea?