Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

javascript - click is not a function error

i am creating a confirmation box where users will click delete and it will ask then if they are sure or not.

this is the html

    <a href="" class="delete"  >Delete</a>

this is the JS handler

        (function($){
              var deleteBox = '<span class="deleteBox"><p>Are you sure you want to delete?</p><span class="cancel">Cancel</span><span class="confirm">Yes</span></span>';
              $(document).on('click', '#deleteproduct', (function(){
                $(this).append(deleteBox);
              }).click(function(){
                if(!$(this).hasClass('selected')){
                  $(this).addClass('selected');
                  var owner = $(this);

                  $(this).find('.cancel').unbind('click').bind('click',function(){
                    owner.removeClass('selected');
                    return false;
                  })

                  $(this).find('.confirm').unbind('click').bind('click',function(){
                    $(this).parent().addClass('loading');
                    var parent = $(this).parent();

                    //ajax to delete

                    setTimeout(function(){ //On success
                      parent.addClass('deleted');
                      setTimeout(function(){
                        owner.fadeOut(600);

                        //remove item deleted

                        setTimeout(function(){
                          owner.find('.deleted').removeClass('loading').removeClass('deleted');
                          owner.removeClass('selected');
                          owner.show();
                        },1000) 
                      },1000)
                    },1000)

                    return false;
                  })
                }   
                return false;
              }));
      })(jQuery);

this is the error its showing

   TypeError: (intermediate value).click is not a function at this line   ......  }).click(function(){

how can I fix this error?

like image 237
Ola Avatar asked Sep 18 '26 21:09

Ola


1 Answers

You are calling .click on (function(){}), but the function-declaration in $(document).on('click', '#deleteproduct', (function(){...}) doesn't return anything with a function click. Instead, you could make something like the following, which adds the html when Delete product is clicked and uses jQuery to add click-events to the html.

var deleteBox = '<span class="deleteBox"><p>Are you sure you want to delete?</p><span class="cancel">Cancel</span> <span class="confirm">Yes</span></span>';

$('#deleteproduct').click(function() {
  var $result = $('#result');
  var $popup = $('#popup');
  $result.text("");
  $popup.append(deleteBox);
  $popup.find('.cancel').click(function () {
    $popup.html("");
    $result.text("Cancelled");
  });
  $popup.find('.confirm').click(function () {
    $popup.html("");
    $result.text("Confirmed");
  });
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<button id="deleteproduct">Delete product</button>
<div id="popup"></div>
<div id="result"></div>
like image 141
Marco de Zeeuw Avatar answered Sep 21 '26 10:09

Marco de Zeeuw



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!