0

Below is how my edit and delete links are generated

function getRecords(){            
    $.getJSON( "viewcustomers.php", function(data) {
        var items = [];
        var xTd = '';
        var xTr = '';
        $.each(data, function (key, val) {
            var c=0;
            var id=0;
            $.each(val, function (key1, val1) {
                if(c == 0) {
                    id = val1;  
                }
                c++;
                xTd += '<td>' + val1 + '</td>';
            });
            xTd += '<td><a href="customers.php?update='+id+'" id="update">Edit</a></td>';
            xTd += '<td><a href="customers.php?delete='+id+'">Delete</a></td>';
            xTr = '<tr>' + xTd + '</tr>';
            items.push(xTr);
            xTd = '';
            xTr = '';
        });
        $("#records").append(items);
    });
}

Now as you can see i have used id=update in my Edit url.

Now after the record is correctly appearing on my page i am trying to run a code when my edit link is clicked .

// Update existing customer
$("#update").click(function(){
    alert("working");
});

The above piece of code is not working. I have tried placing it inside the document.ready as well as outside but nothing worked.

Can anyone tell me what is wrong here?

Ashley Medway
  • 7,151
  • 7
  • 49
  • 71
Raheel
  • 8,716
  • 9
  • 60
  • 102
  • 1
    possible duplicate of [Event binding on dynamically created elements?](http://stackoverflow.com/questions/203198/event-binding-on-dynamically-created-elements) – Ashley Medway Jan 04 '14 at 09:07

4 Answers4

2

Since the #update element are created dynamically you need to use event delegation to register event handlers to these elements.

When you use $('#update').click(....); to register an event handler it will register the handle to only those elements which are already present in the dom at the time of the code execution, in you case since these elements are created after that the handlers will not get attached to the newly created elements

Try with this

HTML

<a href="customers.php?update='+id+'" class="urclassname">Edit</a>

Script

 $("#records").on('click', '.urclassname', function () {
  alert("working");
  });
Sridhar R
  • 20,190
  • 6
  • 38
  • 35
1

Try this

Change id to class ,id should be unique

 xTd += '<td><a href="customers.php?update='+id+'" class="update">Edit</a></td>';
                     ________________________________^_________________

$(document).on('click', '.update', function() {
    alert("working");
});
Pranav C Balan
  • 113,687
  • 23
  • 165
  • 188
1

You need to use event delegation also since there are multiple elements use class for the elements like <a href="customers.php?update='+id+'" class="update">Edit</a>

then

$("#records").on('click', '.update', function () {
    alert("working");
});
Arun P Johny
  • 384,651
  • 66
  • 527
  • 531
0

click not work with dynamically generated elements. As it is bind with elements on body load and element which appended after that do not trigger the event, for this you need event delegation. so try:

$("#records").on('click', '.update', function () {
    alert("working");
}); 
Zaheer Ahmed
  • 28,160
  • 11
  • 74
  • 110