0

I am trying to develop a shopping cart and i am stuck with the following issue. i am creating a new product dynamically using jQuery. The following piece of code adds new product to the cart:

        var productAdded = $('<tr class="product" data-id_modal="'+ id +'"><td class="product_name2"><button href="#0" class="button2" id="'+ id +'">' + product_name + '</button></td><td class="quantity"><span class="select"><select id="cd-product-'+ id +'" name="quantity"><option value="1">1</option><option value="2">2</option><option value="3">3</option></select></span></td><td class="price">' + product_price + '</td><td><a href="#0" class="delete-item"><i class="fa fa-trash-o fa-2x" aria-hidden="true"></i></a></td></tr>');
    cartList.prepend(productAdded);

As you can see above i am creating a new 'button' within javascript. For that element click event is not firing.

$(".button2").click(function(e) {
                    e.preventDefault();

                    $("#add_product_comment").dialog({
                      buttons : {
                          "Άκυρο" : function() {
                          $(this).dialog("close");
                        },
                        "Αποθήκευση" : function() {
                          setInterval(function(){}, 1000); 
                          $( "#add_product_comment" ).dialog( "close" );
                          // $('#two').load(document.URL +  ' #two'); using ajax reload div                           
                        }
                      }
                    });

                    $("#add_product_comment").dialog("open");
                    $('.ui-widget-overlay').css('background', '#000000');

                  });

Any help will be really appreciated.

Omar Ali
  • 7,807
  • 4
  • 29
  • 56
A.A. PEACE
  • 49
  • 6

1 Answers1

1

Use on click instead.

$(document).on('click','.button2',function(){
    //your action.
});
Sarath Kumar
  • 988
  • 13
  • 34