33

How do I change the content of the cell containing the 'c' letter in the HTML sample using jQuery?

<table id="table_header">
<tr>
   <td>a</td>
   <td>b</td>
   <td>c</td>
</tr>
</table>

Thanks.

Flip
  • 6,233
  • 7
  • 46
  • 75
abenci
  • 8,422
  • 19
  • 69
  • 134

6 Answers6

33
$("td:contains('c')").html("new");

or, more precisely $("#table_headers td:contains('c')").html("new");

and maybe for reuse you could create a function to call

function ReplaceCellContent(find, replace)
{
    $("#table_headers td:contains('" + find + "')").html(replace);
}
hunter
  • 62,308
  • 19
  • 113
  • 113
17

Using eq() you can target the third cell in the table:

$('#table_header td').eq(2).html('new content');

If you wanted to target every third cell in each row, use the nth-child-selector:

$('#table_header td:nth-child(3)').html('new content');
Dave Jarvis
  • 30,436
  • 41
  • 178
  • 315
user113716
  • 318,772
  • 63
  • 451
  • 440
12

You can use CSS selectors.

Depending on how you get that td, you can either give it an id:

<td id='cell'>c</td>

and then use:

$("#cell").text("text");

Or traverse to the third cell of the first row of table_header, etc.

Igor
  • 33,276
  • 14
  • 79
  • 112
3

I wanted to change the column value in a specific row. Thanks to above answers and after some searching able to come up with below,

var dataTable = $("#yourtableid");
var rowNumber = 0;  
var columnNumber= 2;   
dataTable[0].rows[rowNumber].cells[columnNumber].innerHTML = 'New Content';
PRTJ
  • 980
  • 1
  • 8
  • 15
2

i was looking for changing second row html and you can do cascading selector

$('#tbox1 tr:nth-child(2) td').html(11111)
Iman
  • 17,932
  • 6
  • 80
  • 90
-4

You can do this :

<table id="table_header">
<tr>
   <td contenteditable="true">a</td>
   <td contenteditable="true">b</td>
   <td contenteditable="true">c</td>
</tr>
</table>
PutihTua
  • 112
  • 2
  • 4
    They wanted to know how to change the value using jQuery, not what changes they should make to the XML. – Steve Oct 03 '13 at 19:07