skip to Main Content

Here is my current data:

<tr id="1">
<th>Name</th>
<td>John</td>
<th>Country</th>
<td>US</td>
</tr>

I tried the following:

let rowID = 1;
let columnTitle = "Country";
$('#'+rowID+' td:eq('+columnTitle+')').html("Japan");

Here is what I expect:

<tr id="1">
<th>Name</th>
<td>John</td>
<th>Country</th>
<td>Japan</td>
</tr>

2

Answers


  1. with JQuery you can do this approach:

    let rowID = 1;
    let columnName = "Country";
    
    // and the fun part is here
    $(`#${rowID} th:contains('${columnName}')`).next('td').text("Japan");
    
    Login or Signup to reply.
  2. Since you need the next td data after th contains country, you need to modify your code like below:

    $("#" + rowID + ' th:contains(' + columnName + ')').next('td').html("Japan");
    

    Working sample:

    let rowID = 1;
    let columnName = "Country";
    
    $("#" + rowID + ' th:contains(' + columnName + ')').next('td').html("Japan");
    <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
    <table>
      <tr id="1">
        <th>Name</th>
        <td>John</td>
        <th>Country</th>
        <td>US</td>
      </tr>
    </table>
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search