0

I want to get text in div inside second td in tr (firstName in my example). Here is what I tried but it give me null.

I tried all below but no one is working.. here is complete code (this table is in popup):

$(document).ready(function() {
$('#listings_row_landlord').dataTable();
});

and this code for alert:

var first_name  = $('#'+value).find("td:eq(1) div").html(); 

<table width="100%" cellspacing="0" cellpadding="0" align="center" style=" border-radius:0px;" id="listings_row_landlord" class="display">
  <thead bgcolor="#E5E5E5" align="left" style="font-size:12px;" class="listing_headings">
  <tr><th width="20" style="text-align:center; width:20px;" class="sorting_desc" rowspan="1" colspan="1"></th><th class="sorting" rowspan="1" colspan="1" style="width: 94px;">First Name</th><th class="sorting" rowspan="1" colspan="1" style="width: 92px;">Last Name</th><th class="sorting" rowspan="1" colspan="1" style="width: 86px;">Mobile No</th><th class="sorting" rowspan="1" colspan="1" style="width: 83px;">Phone No</th><th class="sorting" rowspan="1" colspan="1" style="width: 50px;">Email</th><th class="sorting" rowspan="1" colspan="1" style="width: 95px;">Created By</th><th class="sorting" rowspan="1" colspan="1" style="width: 108px;">Assigned To</th><th class="sorting" rowspan="1" colspan="1" style="width: 59px;">Listed</th></tr></thead>


<tbody>
<?php
             while ( $rw = $oAppl->row($rs) ) {
             ?>


<tr id="<?php echo $rw["id"]; ?>" class="listing_rows odd"><td class=" sorting_1"><div id="item_action" style="text-align:center; width:22px;"><input type="radio" value="<?php echo $rw["id"]; ?>" id="checkbox_<?php echo $rw["id"]; ?>" name="select_landlord"></div></td>

<td>

<div style="width:100px;"><?php echo $rw["fname"]; ?> </div></td>

<td><div style="width:100px;"><?php echo $rw["lname"]; ?></div></td><td><div style="width:160px;"><?php echo $rw["mobile"]; ?></div></td><td><div style="width:150px;"><?php echo $rw["phone"]; ?></div></td><td><div style="width:180px;"><?php echo $rw["email"]; ?></div></td><td><div style="width:100px;">Admin Admin</div></td><td><div style="width:100px;">Admin Admin</div></td><td><div style="width:80px;"><?php echo $rw["created_date"]; ?></div></td></tr>
 <?php
               }
               ?>
</tbody></table>
4
  • you're not closing your <input> tag Feb 27, 2013 at 7:52
  • 2
    $('td:last div', '#'+value).text();
    – adeneo
    Feb 27, 2013 at 7:56
  • like that ^ short and sweet! Feb 27, 2013 at 7:59
  • jsfiddle.net/JdUqR/1 is also working for me but dynamically not working
    – Methew
    Feb 27, 2013 at 8:19

5 Answers 5

1

Use eq() which is zero-based index so you need to use index 1 for second td instead of 2.

Live Demo

var first_name  = $('#'+value).find("td:eq(1) div").html(); 
4
  • 1
    what is the value of value here: '#' + value <-- you sure that is being set? Feb 27, 2013 at 7:57
  • yes this give me correct result..if i click on first,it alert me 1 and for second tr it is 2.....
    – Methew
    Feb 27, 2013 at 7:58
  • my table id = listings_row_landlord,do we need to specify it,as i have alot of tables on page
    – Methew
    Feb 27, 2013 at 8:00
  • i edit my post and provide my whole table html...can u pls check it again?
    – Methew
    Feb 27, 2013 at 8:14
0
 $('#listings_row_landlord #'+value).find("td:eq(1)").text();
3
  • Are you sure value is 1 ?
    – coolguy
    Feb 27, 2013 at 7:58
  • Whats your table id/class ?
    – coolguy
    Feb 27, 2013 at 8:01
  • table id=listings_row_landlord
    – Methew
    Feb 27, 2013 at 8:02
0

this is a little shorter, but adil & adeneo are very concise, imho.

var first_name  = $('#'+value).find("td:eq(1) > div").html(); 
0

try something like this , also check this fiddle

HTML code

  <table>
    <tr id="1" class="listing_rows odd">
       <td class=" sorting_1">
          <div id="item_action" style="text-align:center; width:22px;">
           <input type="radio" value="1" id="checkbox_1" name="select_landlord" />
          </div>
      </td>
      <td>
          <div style="width:100px;">firstName </div>
     </td>
  </tr>
</table>

JS code

  $(function(){
    alert($('#1 td:nth-child(2) div').html());
  })

CHECK this fiddle with your code

0

I resolved the issue.statically it was working well but dynamicall it was not...i just give dynamic id to div and get its html...which worked fine..thanks all for support

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.