Search code examples
jqueryeventslisteners

jquery events on dynamically loaded content


I am trying to add a click event on some dynamically loaded content and I have this:

        $("#dailyCount table thead th").each(function(){
        var yd = "#" + $(this).attr("id");
        $(document).on("click", yd, function(){
            alert("a");
        });

    });

The element id is from 1 to the last day of current month. Any ideas ?


Solution

  • Instead of add a delegated handler for each element you can use one delegation to all elements returned from your selector.

    So try:

    $(document).on("click", "#dailyCount table thead th", function(){
        alert("a");
    });
    

    Demo: http://jsfiddle.net/agLBm/