I know that similar questions are being asked (like here) but, being a jQuery noob, I still can not attach a click listener to an a
element within a li
where the whole ul.li.a
is dynamically appended to the DOM like this.
<div id="users-col">
<!-- dynamically appended -->
<ul id="users-list">
<li>
<a class="user" id="Alice">Alice</a>
</li>
<li>
<a class="user" id="Bob">Bob</a>
</li>
</ul>
<!-- END dynamically appended -->
</div>
Here is a (one of the many) jQuery functions that I have tried:
$('#users-col ul.li.a').on('click', '.user', (function () {
console.log('user clicked:', this.id);
});
How can I fix this?