1

My click event handler is not firing on newly appended list items.

Here are my list items markup:

<ul class="list-inline product-colours">
     <li id="my-sku-1" class="available">product colour 1</li>
     <li id="my-sku-2" class="available selected">product colour 2</li>
     <li id="my-sku-3" class="available">product colour 3</li>
     <li class="not-available">product colour 4</li>
</ul>

My styling for the above list items:

.product-colours li
{
     cursor: pointer;
     padding: 5px;
}

.product-colours .available
{
     border: 2px solid #999;
}

.product-colours .selected
{
     border: 2px solid #333;
}

.product-colours .not-available
{
     border: 2px solid #f00;
     cursor: not-allowed;
}

I am playing around with these list items. When I click on a list item, I then execute some server code in C# and return a JSON result. This result I take and add additional items to the above already created list items.

Here is my jQuery code for the click event handler:

$('.product-colours li').click(function () {
     if (this.id) {
          $.ajax({
               data: { sku: this.id },
               dataType: 'json',
               url: '/MyWebsite/Product/Test'
          }).done(function (data) {
               var productColours = $('ul.product-colours');
               $.each(data.Products, function (i, productColour) {
                    if (productColour.IsAvailable) {
                         if (productColour.IsSelected) {
                              productColours.append('<li id="' + productColour.SKU + '" class="available selected">' + productColour.Name + '</li> ');
                         }
                         else {
                              productColours.append('<li id="' + productColour.SKU + '" class="available">' + productColour.Name + '</li> ');
                         }
                    }
                    else {
                         productColours.append('<li class="not-available">' + productColour.Name + '</li> ');
                    }
               });
          }).fail(function () {
               alert("error");
          });
     }
});

After a new list item is added by doing productColours.append() it displays correctly. When I go and click on the appended list item the click is not picked up by the event handler. The event handler is fired from the items added when the page loaded, but it is not firing on the newly added items. Why is this?

Brendan Vogt
  • 25,678
  • 37
  • 146
  • 234
  • 1
    Use `$('ul.product-colours').on('click', 'li', function() {` Event delegation http://learn.jquery.com/events/event-delegation/ – Tushar Jul 21 '15 at 12:51

1 Answers1

2

Replace:

$('.product-colours li').click(function () {

with

$('.product-colours').on("click", "li",function() {

This is called event delegation: http://api.jquery.com/on/

Delegated events have the advantage that they can process events from descendant elements that are added to the document at a later time. By picking an element that is guaranteed to be present at the time the delegated event handler is attached, you can use delegated events to avoid the need to frequently attach and remove event handlers. This element could be the container element of a view in a Model-View-Controller design, for example, or document if the event handler wants to monitor all bubbling events in the document. The document element is available in the head of the document before loading any other HTML, so it is safe to attach events there without waiting for the document to be ready.

where your original code uses direct event handlers.

Event handlers are bound only to the currently selected elements; they must exist at the time your code makes the call to .on(). To ensure the elements are present and can be selected, place scripts after the elements in the HTML markup or perform event binding inside a document ready handler. Alternatively, use delegated events to attach event handlers.

Khanh TO
  • 48,509
  • 13
  • 99
  • 115