-2

i am just practising in some king of exercise and stuck in a point i dont understand.in my code i got a button and when the user press it ,it creates 25 cells and place random numbers on them.After i tried to do some kind of things when the user clicks in each cell.Well it didnt work.the $("td") selector doesnt trigger at all.any help?

$("button").click(function(){
    $("body").append("<p></p>")
    for (i=0; i<5; i++){
        $("#t1").append("<tr></tr>");
    }
    for (i=0; i<5; i++){
        $("#t1 tr").append("<td></td>");
    }
    for (i=0; i<25; i++){
        var z = Math.round((Math.random()*89)+10);
        $("#t1 tr td").eq(i).text(z);
    }
});

$("td").click(function(){
    //code goes here
});
Mohammad
  • 19,228
  • 14
  • 49
  • 73
Alator
  • 487
  • 6
  • 21
  • What HTML are you working with? Are there any errors? @dotnetom: I'm not sure about that dupe, I may be tired and missing something, but it seems that the `` elements are being created after the ` – David says reinstate Monica Jun 07 '16 at 19:23

1 Answers1

0

Instead of the regular .click() method, you should use .on() on an element that already exists, but that will contain the td later, for example $(document) or the table your cells will appear in.

$(document).on('click', 'td', function() {
    //code here
});

jQuery .on() documentation

Stephan Muller
  • 24,574
  • 14
  • 80
  • 119