[Solved] jquery click() function not working [duplicate]


You need to bind the click handler after the element is loaded,

$.getJSON("data.php", function(data) {
    var items = [];
    $.each(data, function(i, val) {
        $("#" + i).html("<a href="https://stackoverflow.com/questions/25967816/javascript:void(0);" class="hov">" + val + "</a>");
    });
    $('.hov').click(function() {
        alert("Handler for .click() called.");
    });
});

Other option is to use delegates. Then your code will be like,

$.getJSON("data.php", function(data) {
    var items = [];
    $.each(data, function(i, val) {
        $("#" + i).html("<a href="https://stackoverflow.com/questions/25967816/javascript:void(0);" class="hov">" + val + "</a>");
    });

});
$(document).on('click', '.hov', function() {
    alert("Handler for .click() called.");
});

solved jquery click() function not working [duplicate]