Victor Bojica
Victor Bojica

Reputation: 333

Jquery selector not functioning on td

Im havin problems in selecting my first 2 td of every tr(i need to make them lickable) and my last td of every tr(different links). Can someone help me with the code? I cant seem tofigure it out. The code seems legit but it doesnt work. Here is the js and html:

JS

$(".rand_notif td:lt(2)").click(function(){
    $(".rand_notif").html("asd");
})

HTML (somehow..still js but html)

$.each(data.notif,function(i,x){
    var cant='';

    if(x.cant>0){var cant = x.cant+"x";}
    notificari+="<tr class='spacer_2'></tr><tr class='notificari rand_notif' record='"+x.id+"'><td>"+cant+"</td><td>"+x.nume+"</td><td>Refuz</td></tr>";
});

Actual html

<table cellspacing="0" id="tabel_notificari">
    <tr class="spacer_1"></tr>
    <tr class="table_head notificari">
        <th width="30"></th>
        <th>Notificari</th>
        <th width="86"></th>
    </tr>
</table>

EDIT: Problem solved. The problem was, as explained in the comments, the fact that the elements were first binded then added, therefore the bind didnt exist. Solution:

$("#tabel_notificari").on("click", ".rand_notif td:lt(2)",function(){$(".rand_notif").html("asd");});

Upvotes: 2

Views: 105

Answers (2)

Calvin Allen
Calvin Allen

Reputation: 4248

You are creating the table cells via javascript, so you need to use a different method to attach the click event to them. Try using the "ON" method attached to the table itself, as shown below. This will apply to any TD added to the table after the DOM loads.

$("#tabel_notificari").on("click", ".rand_notif td:lt(2)", function(e) {
   $(this).html("asd");
});

Upvotes: 2

Jim Cote
Jim Cote

Reputation: 1756

There are no TD tags in your HTML. Did you mean TH?

Upvotes: 1

Related Questions