0

I am adding dynamically hyperlink and want to execute click event as below when ever user clicked on it but it is not firing up.

 <table id="assinged_areas" class="table table-bordered table-condensed table-condensed">
            <tbody></tbody>
        </table>

When user selected some option in DropDown then it goes and get areas from server then ajax get calls this function by passing data to this function

 function updateAssignedAreas(data) {

            debugger;
            var areas = data.split(',');
            $.each(areas, function (i, item) {
                $('#assinged_areas > tbody:last').append('<tr><td>' + item + '</td><td>' + '<a class=\'btn btn-danger\'><i class=\'fa fa-minus-circle\'></i></a>'
                    + '</td></tr>');
            });
        }

This is the function when user click on unassign must call but not firing at all

  $('.btn-danger').click(function (parameters) {
            debugger;
        });
ineffable p
  • 1,207
  • 2
  • 22
  • 46

1 Answers1

1

Use event delegation for dynamic elements

$("#assinged_areas").on("click", '.btn-danger', function (parameters) {
    var row=$(this).closest("tr");
    alert($(this).closest("tr").find("td").eq(0).html()); //will get the inner text of first td
});

Event delegation allows you to attach a single event listener, to a parent element, that will fire for all children matching a selector, whether those children exist now or are added in the future.

Anoop Joshi P
  • 25,373
  • 8
  • 32
  • 53