Change cell contents from another cell

hey, I'm trying to select change the contents of a cell with jquery, but I can't get it to work. let's say we have this table:

<table border="1">
<tr>
<td>cell 1
<td>cell 2
<td>cell 3
<tr>
<td>cell 4
<td>cell 5
<td>cell 6 <a onclick="javascript:$(this).parent().parent().('td').html('test');">Click</a>
<tr>
<td>cell 7
<td>cell 8
<td>cell 9
</table>

I want to change the contents of cell 4 from inside cell 6. Obviously ('td') is not working. How can I access cell 4 from cell 6 without using classes or identifiers? thanks in advance

+3
source share
3 answers

You want it because you want to go up one and go back.

$(this).parent().prev().prev().html('test');

or like this:

$(this).parent().prevAll(':last').html('test');

or that:

$(this).parent().siblings(':first-child').html('test');

... or you can do it pretty easily without jQuery:

this.parentNode.parentNode.cells[0].innerHTML = 'test';
+5
source

Change javascript as:

$ (this) .parent () parent () children ('td') ek (0) .html ('test'); ...

td ( 4) HTML.

0

If you want to change the 4th <td>in your table, you can go to the table closest, and then the findfourth <td>inside it. Then you can change your HTML.

$(this).closest('table').find('td:eq(3)').html('test');

Live example

0
source

Source: https://habr.com/ru/post/1785635/


All Articles