youv youv - 4 months ago 14
Javascript Question

Getting table tr id undefined

Am getting the return value of a table tr:last id undefined

<table id="tableid">
<thead>
<th>sl.no</th>
<th>Name</th>
<th>Place</th>
</thead>
<tbody>
<tr id="1"><td>1</td>
<td>Roy</td>
<td>UK</td></tr>
<tr id="2"><td>2</td>
<td>Doe</td>
<td>US</td></tr>
</tbody>
</table>

$("#tableid tbody tr:last").attr('id');


Please help,
Thanks

Answer

your code is working fine.check below

   <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.10.2/jquery.min.js">
    </script>  

<script>
$(document).ready(function(){
var lastid=$("#tableid tbody tr:last").attr('id');  
alert(lastid); 
});   
   </script> 




 <table id="tableid" >
 <tbody>  

 <tr id="trid1"></tr> 
   <tr id="trid2"> </tr>
    <tr id="trid3"></tr>
 </tbody>
 </table >
Comments