I'm trying get the ID of appended list element. After trying out for hours and reading multiple sources from jquery get the id/value of LI after click function and How to get the id of html list item using jquery?, I still couldn't figure it out the bug inside my code.
The only difference between the above sources and what I'm trying to achieve is they already have list element present inside html where as I'm appending list through jquery function.
Here is my code.
Html
<button id="display">Display</button>
<div class="main">
<ul>
</ul>
</div>
<p id="show"> </p>
Jquery 2.1.0
var i = 1;
$('#display').click(function() {
$(".main ul").append(
$('<li>').attr("id",i).append(
$('<p>').append("Stackoverflow")
));
i++;
});
$('.main ul li').click(function () {
$('#show').text("List ID =" + this.id);
});
Thank you for your time.