jQuery event not triggering on dynamically created element

Raghib Ahsan

Here goes my question.. enter image description here

/* I am using ajax to dynamically create table */

 $(".n").click(function(){
      var id= $(this).closest('tr').find('td.ide2').html();

         //for displaying the table
         $.ajax({
           type: 'POST',
           url: '<?php echo base_url(); ?>Admin/show', //We are going to make the request to the method "list_dropdown" in the match controller
           dataType:'json',
           data: {'id':id}, //POST parameter to be sent with the tournament id
           success: function(resp) { 

             for(var i=0;i<(resp.length);i++)
              {
                var row = $('<tr></tr>').appendTo($("#unique-list"));

                $('<td />',{text:resp[i]}).appendTo(row);
                $('<td class="off-del glyphicon glyphicon-minus"></td>').appendTo(row);  

             }//end for loop
            } //end success
            });  //end ajax




          $(".off-del").click(function(){
          alert('hello');
          var id= $(this).closest('tr').find($(":first-child")).html();
          console.log(id);
          });
        });

the event click on $(".off-del") is not triggering automatically I have to write in console the name of the event then this event starts functioning. Is there any issue with class name generating dynamically and how to overcome

After i wrote the name of the event in console it works enter image description here

Shaunak D

Ajax calls are asynchronous.

Before the elements are appended via ajax, the click handler gets registered, which find no elements with $(".off-del").

You should probably use event delegation.

$(document).on('click','.off-del',function(){
    alert('hello');
    var id= $(this).closest('tr').find($(":first-child")).html();
    console.log(id);
});

Instead of $(document) you can use a static parent.

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Java

jquery - Click event not working for dynamically created button

From Dev

Add data to dynamically created element and bind event

From Dev

Getting id of dynamically created element jQuery

From Dev

Add Event Listener for dynamically created particular element using jquery

From Dev

Jquery Event Not Triggering for DOM Elements Created after page load

From Dev

Jquery click event not firing on the element created dynamically using jquery

From Dev

jQuery Tooltip event not firing on dynamically created element

From Dev

Jquery : Can't remove element dynamically created

From Dev

jQuery - mousemove on dynamically created element

From Dev

JQuery - Dynamically created element css not working

From Dev

cllick event of dynamically created Checkbox in Jquery

From Dev

Dynamically created element not firing click event

From Dev

dynamically created li click event not working jquery

From Dev

Jquery: Get attribute value of dynamically created element

From Dev

JQuery: on input event on dynamically created inputfields

From Dev

jQuery - dynamically created element as selector with 'eq()'

From Dev

Dynamically Created Element and event

From Dev

Binding event to dynamically created element

From Dev

jQuery Selector not working for a dynamically created element

From Dev

jquery dynamically created element not triggering onchange

From Dev

jQuery Tooltip event not firing on dynamically created element

From Dev

jQuery - Add click event to a dynamically created element

From Dev

Why does this dynamically created element not bound to an event?

From Dev

Dynamically created element not firing click event

From Dev

Create a function or event for dynamically created fields jQuery

From Dev

Dojo - add event listener to dynamically created element

From Dev

jquery - add event listener to dynamically created button

From Dev

Event Binding on Dynamically Created Element

From Dev

jquery listen to click event on dynamically created element

Related Related

  1. 1

    jquery - Click event not working for dynamically created button

  2. 2

    Add data to dynamically created element and bind event

  3. 3

    Getting id of dynamically created element jQuery

  4. 4

    Add Event Listener for dynamically created particular element using jquery

  5. 5

    Jquery Event Not Triggering for DOM Elements Created after page load

  6. 6

    Jquery click event not firing on the element created dynamically using jquery

  7. 7

    jQuery Tooltip event not firing on dynamically created element

  8. 8

    Jquery : Can't remove element dynamically created

  9. 9

    jQuery - mousemove on dynamically created element

  10. 10

    JQuery - Dynamically created element css not working

  11. 11

    cllick event of dynamically created Checkbox in Jquery

  12. 12

    Dynamically created element not firing click event

  13. 13

    dynamically created li click event not working jquery

  14. 14

    Jquery: Get attribute value of dynamically created element

  15. 15

    JQuery: on input event on dynamically created inputfields

  16. 16

    jQuery - dynamically created element as selector with 'eq()'

  17. 17

    Dynamically Created Element and event

  18. 18

    Binding event to dynamically created element

  19. 19

    jQuery Selector not working for a dynamically created element

  20. 20

    jquery dynamically created element not triggering onchange

  21. 21

    jQuery Tooltip event not firing on dynamically created element

  22. 22

    jQuery - Add click event to a dynamically created element

  23. 23

    Why does this dynamically created element not bound to an event?

  24. 24

    Dynamically created element not firing click event

  25. 25

    Create a function or event for dynamically created fields jQuery

  26. 26

    Dojo - add event listener to dynamically created element

  27. 27

    jquery - add event listener to dynamically created button

  28. 28

    Event Binding on Dynamically Created Element

  29. 29

    jquery listen to click event on dynamically created element

HotTag

Archive