Reputation: 55
I'm trying to make a program that calculates something and inserts the result into the cell next to the equasion in the table. I don't want to give each cell I want to insert the result in different classes because if I had to do this 1000 times then the code would get messy and I would have 1000 different IDs for each cell in the table. Is there a function that gets index or something of a cell and then I can insert the value into that cell? Or the only way to do it is to make these IDs?
Upvotes: 0
Views: 230
Reputation: 178151
You can use relative addressing and a selector
document.querySelectorAll("tr :nth-child(1)")
.forEach(cell => cell.nextElementSibling.textContent = eval(cell.textContent))
td:nth-child(1):after { content:":" }
<table>
<tr>
<td>2+2</td><td></td>
</tr>
<tr>
<td>2*3</td><td></td>
</tr>
<tr>
<td>2-2</td><td></td>
</tr>
</table>
Upvotes: 1