battlenub
battlenub

Reputation: 187

Incrementing td field of tr via id

Want to increment last td field in a doubleclicked tr but can't seem to get it to work. Td's are dynamically added.

$("#addnewitem").click(function () {
$item = $("#selectitem").val();
if ($item == "No selection") {
    alert("No selection");
} else {
    $("#tabelbody").append("<tr id=" + $item + "><td></td><td>" + $item + "</td>"
    + "<td>1</td>" /*WANT TO INCREMENT THIS TD'S VALUE*/
    +"</tr>");
}
 });
 $("#tabel").on('dblclick','tr',function(){
var trid = $(this).attr('id'); // table row ID 
alert(trid);

});

Upvotes: 1

Views: 298

Answers (2)

TheVillageIdiot
TheVillageIdiot

Reputation: 40507

try this:

$("#tabel").on('dblclick', 'tr', function(){
    var t=$(this).find("td:last");
    t.text( parseInt(t.text(), 10) + 1);
});

Upvotes: 1

Edgar Villegas Alvarado
Edgar Villegas Alvarado

Reputation: 18344

You should do:

$("#tabel").on('dblclick','tr',function(){
   var $tr = $(this);    //Get tr
   var $td = $tr.find("td:last");  //Get last td
   var oldValue = parseInt($td.text(), 10);   //Get previous value
   $td.text(oldValue + 1);    //Update value
});

I did it in several steps so you can understand it better

Upvotes: 1

Related Questions