Click here to Skip to main content
Rate this: bad
Please Sign up or sign in to vote.
See more: Javascript jQuery
Hi i am adding and deleting rows dynamically using javascript and its working fine... the adding and deleting of rows to a HTML table. But after adding a few rows if i delete rows inbetween in the table and then add a new row.. there is a mismatch happening.. so i want to retrieve the id of the last row added... i tried many ways to egt id of the last row in the HTML table but i am not able to get.. few methods i tried were.... as follows:
$('#tblrownew0 tr:last td:eq(1)').id
it showed undefined.
$('#tblrownew0 tr:last')
this if i use alert i am getting objectobject but how can i get the id of the row??
Please help!! thanking u in advance!!
Posted 13-Mar-13 4:09am

1 solution

Rate this: bad
Please Sign up or sign in to vote.

Solution 1

Just use jQuery attr method[^] like this:
var id = $('#tblrownew0 tr:last').attr('id')
or you can access any DOM property like this:
var id = $('#tblrownew0 tr:last')[0].id
It is important for you to know, when you are working with jQuery object or DOM elements.
Shruthi.BT at 14-Mar-13 2:18am
thank u thanks a lot!! it worked!! but i have another question... i am actually inserting the values of the table in database and since the user has the facility to delete the records or rows from table ... so i am not able to get the values based on id how can i retrieve the row values without the id means to insert in db... please help!!
Matej Hlatky at 14-Mar-13 6:35am
Have you tried to combine children("td") / find("td > something") with text() jQuery methods?
Please use Improve question link to add more information: what is your current state and what do you want to do.
Shruthi.BT at 14-Mar-13 6:44am
for adding i am using a hidden field concept.. trying that.. if it does not work.. will give out more details of td and all.. for now.. i got naother doubt with this...
var id = $('#tblrownew0 tr:last').attr('id')
var id = $('#tblrownew0 tr:last')[0].id
for this line what if i do not know the tablename and i get that name dynamically in a variable like for ex:
var tablename = "tblrownew0";
then how can i use tablename in this line...
var id = $('#tblrownew0 tr:last').attr('id')
var id = $('#tblrownew0 tr:last')[0].id
Please help!!
i tried like this
var id = $('tablename tr:last').attr('id')
it is showing undefined...
also like this is used and checked...
var id = $('#tablename tr:last').attr('id')
again undefined in alert.., please please help!!
Matej Hlatky at 14-Mar-13 7:06am
Use $('#'+tablename+' tr:last').attr('id') , since tablename is variable, and you want to create jQuery selector "#sometableid tr:last".
Shruthi.BT at 14-Mar-13 7:11am
hey thanks a ton!! u really made my work easier... thank u so much!!

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

  Print Answers RSS
0 DamithSL 320
1 OriginalGriff 235
2 Afzaal Ahmad Zeeshan 154
3 Sergey Alexandrovich Kryukov 147
4 BillWoodruff 135
0 OriginalGriff 7,510
1 DamithSL 5,494
2 Sergey Alexandrovich Kryukov 4,954
3 Maciej Los 4,936
4 Kornfeld Eliyahu Peter 4,514

Advertise | Privacy | Mobile
Web03 | 2.8.141223.1 | Last Updated 13 Mar 2013
Copyright © CodeProject, 1999-2014
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100