1

I am using jquery datatables plugin. Each row has two action buttons to confirm or cancel the item in the table.

I am getting problem to add on click event for both of them. Buttons added directly into the json data.

(function ($){
    $(function (){

        let confirm_button = $('.gs-confirm-order');
        let cancel_button = $('.gs-cancel-order');
    
    
        // cancel order process
        cancel_button.on('click', function (){
            if (confirm('Are you sure you want to cancel this order? Undo is not possible.')) {
            console.log('Item cancelled.');
            } else {
            console.log('Operation cancelled.');
            }
        });
    });
})(jQuery);

within the document ready also not working

(function ($){
    $(document).ready(function (){
        let response_alert = $('#response');
        response_alert.hide();

        let confirm_button = $('.gs-confirm-order');
        let cancel_button = $('.gs-cancel-order');

        // cancel order process
        cancel_button.on('click', function (){
            if (confirm('Are you sure you want to cancel this order? Undo is not possible.')) {
                console.log('Item cancelled.');
            } else {
                console.log('Operation cancelled.');
            }
        });
    });
})(jQuery);
9
  • 1
    Hi , change your selector like this $(document).on('click','.gs-cancel-order',function(){ // your code}) and see if that works. Commented Feb 8, 2021 at 6:09
  • I have tried that too but it is then considering entire document. Even clicking on the page also triggers the event. Commented Feb 8, 2021 at 6:10
  • or like this ? Commented Feb 8, 2021 at 6:11
  • Let me try that way. Commented Feb 8, 2021 at 6:14
  • @Swati there is an issue. If I use variable the even trigger by clicking anywhere on the document but if I pass class itself .gs-cancel-order it works. I can pass the class, but the problem is, I have to refer the same object after AJAX complete. Updating class etc. Commented Feb 8, 2021 at 6:20

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.