Event binding .on() not working, on dynamic buttons

a_man

I've used the method shown in this answer Event Binding on dynamically created elements?

However the problem is in this code this technique is not working

//Event Binding
$("#check_it_trial_0").on('click', '.delete_addr', function() {
  "use strict";
  alert("oo");
});

$(document).ready(function() {
  "use strict";
  $('.d_address .delete_ad').click(function() {
    //var elem = $(this).closest('.d_address');

    $.confirm({
      'message': 'Are you sure, you want to delete this address?',
      'buttons': {
        'Delete': {
          'class': 'delete',
          'action': function() {
            //elem.slideUp();
          }
        },
        'Cancel': {
          'class': 'cancel',
          'action': function() {} // Nothing to do in this case. You can as well omit the action property.
        }
      }
    });
  });
});

(function($) {
  "use strict";
  $.confirm = function(params) {
    if ($('#confirmOverlay').length) {
      // A confirm is already shown on the page:
      return false;
    }

    var buttonHTML = '';
    $.each(params.buttons, function(name, obj) {
      // Generating the markup for the buttons:
      if (obj['class'] === 'delete') {
        buttonHTML += '<button class="delete_addr ' + 'button ' + obj['class'] + '">' + name + '</button>';
      } else {
        buttonHTML += '<button class="cancel ' + 'button ' + obj['class'] + '">' + name + '</button>';
      }
      if (!obj.action) {
        obj.action = function() {};
      }
    });

    var markup = [
      '<div id="confirmOverlay">',
      '<div id = "holding" >',
      '<div id="confirmBox">',
      '<p>', params.message, '</p>',
      '<div id="confirmButtons">',
      buttonHTML,
      '</div></div></div></div>'
    ].join('');
    $(markup).hide().appendTo('#check_it_trial_0').fadeIn();

    var buttons = $('#confirmBox .button'),
      i = 0;

    $.each(params.buttons, function(name, obj) {
      buttons.eq(i++).click(function() {

        // Calling the action attribute when a
        // click occurs, and hiding the confirm.

        obj.action();
        $.confirm.hide();
        return false;
      });
    });
  };

  $.confirm.hide = function() {
    $('#confirmOverlay').fadeOut(function() {
      $(this).remove();
    });
  };
})(jQuery); // JavaScript Document
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<div class='d_address'>
  <span class='_in_b in_ed_de delete_ad'>Delete</span>
</div>
<div id="check_it_trial_0"></div>

When I run this code, the on('click') function don't work. Even here I am using $(staticAncestors).on(eventName, dynamicChild, function() {}); this logic

pyrogrammer

Replace your jquery code with this one.

    $(document).ready(function() {
  "use strict";

  //Event Binding
  $("#check_it_trial_0").on('click', '.delete_addr', function() {
    "use strict";
    alert("oo");
  });

  $('.d_address .delete_ad').click(function() {
    //var elem = $(this).closest('.d_address');

    $.confirm({
      'message': 'Are you sure, you want to delete this address?',
      'buttons': {
        'Delete': {
          'class': 'delete',
          'action': function() {
            //elem.slideUp();
          }
        },
        'Cancel': {
          'class': 'cancel',
          'action': function() {} // Nothing to do in this case. You can as well omit the action property.
        }
      }
    });
  });
});

(function($) {
  "use strict";
  $.confirm = function(params) {
    if ($('#confirmOverlay').length) {
      // A confirm is already shown on the page:
      return false;
    }

    var buttonHTML = '';
    $.each(params.buttons, function(name, obj) {
      // Generating the markup for the buttons:
      if (obj['class'] === 'delete') {
        buttonHTML += '<button type="button" class="delete_addr ' + 'button ' + obj['class'] + '">' + name + '</button>';
      } else {
        buttonHTML += '<button type="button" class="cancel ' + 'button ' + obj['class'] + '">' + name + '</button>';
      }
      if (!obj.action) {
        obj.action = function() {};
      }
    });

    var markup = [
      '<div id="confirmOverlay">',
      '<div id = "holding" >',
      '<div id="confirmBox">',
      '<p>', params.message, '</p>',
      '<div id="confirmButtons">',
      buttonHTML,
      '</div></div></div></div>'
    ].join('');
    $(markup).hide().appendTo('#check_it_trial_0').fadeIn();

    var buttons = $('#confirmBox .button'),
      i = 0;

    $.each(params.buttons, function(name, obj) {
      buttons.eq(i++).click(function() {
        obj.action();
        $.confirm.hide();
      });
    });
  };

  $.confirm.hide = function() {
    $('#confirmOverlay').fadeOut(function() {
      $(this).remove();
    });
  };
})(jQuery); // JavaScript Document

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

Dynamic event binding not firing

Dynamic binding in Knockout click event

How to set event binding for group of buttons in Angular?

jquery binding click event to multiple buttons

Click event on android buttons (binding only once)

Angular directive event binding not working

Data binding not working in an event in directive

Binding ComboBox (source) with Buttons not working properly.

Jquery right event for dynamic loaded buttons

Event handler not working on dynamic content

dynamic linkbuttons click event not working

Jquery Mobile dynamic dialog with listview event not binding

Tkinter dynamic event binding for multiple tags

Dynamic template variable inside event binding

Dynamic binding of typeahead select event (jQuery)

Hover not working correctly on dynamic buttons in Tkinter

jQuery event delegation not working for some buttons

Button click event of a dynamic table with dynamic buttons with jQuery

Clojure binding of dynamic var not working as expected

why property binding in dynamic component creation not working?

Dynamic class binding svg and vue not working

Video element event binding not working in Safari browser

Angular click event binding is not working properly

Event binding to each LinkButton of Repeater not working

Click event not working on button of dynamic generated table

Run time not able to fire the button click event for dynamic buttons

Swift3 Multiple buttons dynamic coding to trigger event

How to add Event with Dynamic Radio Buttons to show a Text Box

Add a Right-Click Event to dynamic created Buttons in c#