user2636556
user2636556

Reputation: 1915

inserting data in td of large table

i have a HTML table that looks somewhat like this

<tr rel="price">
    <th>Price</th>
    <td class="">-</td>
    <td class="">-</td>
    <td class="">-</td>
    <td class="">-</td>
</tr>

how would i insert data the the 4th td? I could give the 4th td an ID and insert like this

$('#id').html('bla bla');

but my table has 60 rows and 4 columns. Personally think it would be abit messy if i was going to add ID's to all td's. Any suggestions? Obviously something that works fast on a big table would be ideal. Thanks

Upvotes: 0

Views: 172

Answers (3)

Valentin Mercier
Valentin Mercier

Reputation: 5326

$("tr[rel=price] td:nth-child(3)").html('bla bla');

3 is because counting starts at 0, so that is the 4th element.

Upvotes: 1

Kartikeya Khosla
Kartikeya Khosla

Reputation: 18873

<tr rel="price" id="tr1">
    <th>Price</th>
    <td class="">-</td>
    <td class="">-</td>
    <td class="">-</td>
    <td class="">-</td>
</tr>

$('table tbody #tr1').closest("td").eq(3).html('bla bla');

Upvotes: 0

Okky
Okky

Reputation: 10466

Try using :nth selector

$('table tbody tr td:nth(3)').html();

or

$('table tbody tr td:nth-child(3)').html();

Upvotes: 5

Related Questions