jquery on click event



  • There's a functionary that dynamically cleans/supplies the goods into the basket, and then the modifications are selected accordingly. The problem now is that after the page is downloaded -> Installation of goods in the basket -> cleaning the same product from the basket. -> Repeat addition of the same product to the basket -> Removal is not effective. When we went through the network, we found out we could tie the event to the subsidiary element, but I just need to tie it to the parent.

    The code is:

     var response = JSON.parse(response);
                $('.box3 .basket .counter').empty().append(response.countBasket);
    
            var image = response.product.images.split(',')[0];
            var newElement = "";
            newElement += '<li class="basket-list-item" data-id="' + productId + '">';
            newElement +=   '<div class="img-frame">';
            newElement +=      '<img src="/site_media/' + image + '/" alt="">';
            newElement +=   '</div>';
            newElement +=   '<h4 class="title">' + response.product.title + '</h4>';
            newElement +=   '<p>' + response.product.material + '</p>';
            newElement +=   '<span>Quantity: <span class="basket-item-quantity">' + response.product.quantity + '</span></span>';
            newElement +=   '<a class="icons-square-close"></a>';
            newElement +=  '</li>';
            basketList.append(newElement);
    

    Event:

    $('.popup-basket a.icons-square-close').on('click', '.basket-list-item', function (e) {
    e.preventDefault();
    var id = $(this).closest('li').attr('data-id');
    var title = $(this).closest('li').find('.title').text();
    removeFromTheBasket(id, title);
    });

    Functions removeFromTheBasket() It's just popping out and cleaning out the basket.



  • Anyway, I didn't hang the processor. a.icons-square-close, a tag. ul.popup-basketthe one that's already caught the tag that was clicking. The new code looks like:

    $('.popup-basket').on('click', function (e) {
    e.preventDefault();
    var target = e.target;
    var li = $(target).closest('li');
    if(li){
        var id = li.attr('data-id');
        var title = li.find('.title').text();
        removeFromTheBasket(id, title);
    }else{
        return;
    }
    });
    



Suggested Topics

  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2