To get TD value using jQuery

I have a very simple table with two rows.
I thought the best way to get the value from TD with id is "row2".

<Table id="testing>
<tr>
<th>
</th>
<td id="row1">hello</td>
</tr>
<tr>
<th>
</th>
<td id="row2">world</td>
</tr>
</table>

Here is my attempt:

$(document).ready(function(){ 
      var r=$("#testing":row2).val();
      alert(r);
});

But I did not see the message appear. What should I do in jQuery code if I want to specify a table id along with a TD id?

 var r=$("#testing":row2).text();
 var r=$("#testing").children("row2").text();
+5
source share
4 answers

This will do it for you:

  var r = $("#testing #row2").text();
  alert(r);

In action here for your pleasure.

+21
source

Use text()insteadval()

var r = $("#row2").text();

Additional Information:

+4
source

TD . TD . , , TD ID, : ( )

 <table id="test1">
    <tr>
    <th>
    </th>
    <td id="test1_row1">hello</td>
    </tr>
    <tr>
    <th>
    </th>
    <td id="test1_row2">world</td>
    </tr>
 </table>

?

0
    <table>
<tr>
    <td class="tdcls">1</td>
    <td class="tdcls">2</td>
    <td class="tdcls">3</td>
</tr>
<tr>
    <td class="tdcls">4</td>
    <td class="tdcls">5</td>
    <td class="tdcls">6</td>
</tr>                   

jquery code to select a specific td value

$(".tdcls").mouseenter(function(){
    var a = $(this).text();
});
0
source

All Articles