I am adding list items w/ child anchor tags to a unordered list depending upon what is returned from an AJAX request. These exist inside of a drop down and require the unique text value returned for when each specific anchor is clicked. x is the unique value that each item is being created for in the script bellow.
var appendDropDwn = function appendDropDwn(x) {
console.log("Append Drop Down");
var html = "<li><a href='#' class='x-list' id="+ x + ">" + x + "</a></li>"
console.log(html);
$("#x-dropdown-ul").append(html);
}
I then create an event listener using the class in the append function above,
$(".x-list").click(function (e) {
var y = $(this).text();
alert(y);
});
The issue is that when the anchor element is clicked, the script runs for all anchor tags. In other words if their is 100 list item/anchor tags appended to the UL in the first script, then the second script runs 100 times but only alerts the .text() of the one actually clicked. It alerts the same text value 100 times.