I am using jquery 2.1.4
I dynamically create a list with hyperlinks to trigger a function.
$.getJSON('json_data.php', { method: 'getCertifications', userId: userId }, function(data) {
$.each(data, function(key, value) {
i++;
$("#certificationCount").text("Total - " + i);
$("#userCertifications").append('<div class="usercert"><li>' + value.certName + '</li><a href="#" data-id="' + value.certName + '">X</a></div>');
});
});
Then I want to call a function when a user clicks on the "X" hyperlink. However, the function is not being called.
$(document).ready(function() {
$(".usercert a").on('click', 'a', function(e) {
console.log("It works!");
alert("It works!");
});
});
Why is the function inside the document ready not firing?
.usercert aas your base element(s) for your delegated event handler. Are you sure that the element(s) exist when the binding occurs? If not, bind to an element that exists in the DOM when the binding occurs and use.usercert aas your delegated selector.