Click here to Skip to main content
15,903,724 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
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

1 solution

Just use jQuery attr method[^] like this:
JavaScript
var id = $('#tblrownew0 tr:last').attr('id')
or you can access any DOM property like this:
JavaScript
var id = $('#tblrownew0 tr:last')[0].id

It is important for you to know, when you are working with jQuery object or DOM elements.
 
Share this answer
 
Comments
Shruthi.BT 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 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 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 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 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)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900