Delegating Events Instead of Direct Binding in DOM

One thought on “Delegating Events Instead of Direct Binding in DOM”

  1. this would be more efficient —
    ;(function($){
    $(document).ready(function(){
    var $container = $(“div.container”), //use the div to find container to prevent jquery from searching all elements for class of container.
    buttons = 1; //cache this
    //event delegation
    $container
    .on(“click”,”button”,function(){
    alert(“Clicked”);
    })
    .on(“click”,”#addmore”, function(){
    $(“”).html(“Click Me ” + (++buttons)).appendTo($container);
    });
    });
    })(jQuery);

Leave a Reply

Your email address will not be published. Required fields are marked *