0

I started learning jquery few days ago and now i'm facing one problem in retrieving values from table using jquery

<table id="tblsample">
  <tr>
    <td id="1">Data one</td>
    <td id="2">Data two</td>
  </tr>
  <tr>
    <td id="3">Data one</td>
    <td id="4">Data two</td>
  </tr> 
<tr>
    <td id="4">Data one</td>
    <td id="5">Data two</td>
  </tr>
  .
  .
  .

</table>

if i click on the first tr row i want to read "2"

 <td id="2">Data two</td>

This is for binding a dropdown based on value.

for first td's i did something like below only for reading the text part

 $('#tblsample tr td').click(function () {

    var cval = $(this).text();
    $(txtCountry).val(cval);//Binding to textbox - "Data One"

});

How can i take the id's of second td's for each tr.

Sourav Sachdeva
  • 353
  • 6
  • 20
Jithin j
  • 272
  • 1
  • 3
  • 13
  • Possible duplicate of [Get the table row data with a click](https://stackoverflow.com/questions/17356497/get-the-table-row-data-with-a-click) – parlad Apr 22 '18 at 13:18

2 Answers2

0

Simply do $(this).parent() to trigger your tr, and use find :

let nextTdId = parseInt($(this).attr('id')) + 1; //get the next td id
$(this).parent().find(nextTdId); //get the next td

Or you can just select the 2nd child of the parent of your td, or use $(this).siblings()[0], many solutions indeed

aznoqmous
  • 121
  • 1
  • 11
0

With QJuery ,

$("tr.myTable").click(function() {
var tableData = $(this).children("td").map(function() {
    return $(this).text();
}).get();

console.log(tableData);
});
parlad
  • 1,143
  • 4
  • 23
  • 42