You almost certainly don't want an id
on every td
element, but if you really do, here's one way:
// Spin through all rows in the table
$("#yourtable tr").each(function(row) {
// Spin through all cells in the row
$(this).find("td").each(function(col) {
// Assign an ID
this.id = "cell" + row + "x" + col;
});
});
...where #yourtable
is just a placeholder for a selector that matches your table. That assumes you don't have any nested tables.
Again, though, you probably don't want that. You haven't given any details on what's triggering the ajax call, but usually when this sort of thing comes up, it's in relation to a user action on an element in the row where you want the change made (for instance, increasing or reducing a count). If that's your use case, remember the row, and then update the cell within that row rather than doing a search on the document as a whole.
Example:
$("table").on("click", "input[type=button]", function() {
var $btn = $(this),
operation = $btn.val() == "+" ? 1 : -1,
$row = $btn.closest('tr');
// Simulating ajax by using setTimeout...
setTimeout(function() {
var $input = $row.find('input[name="foo[]"]');
$input.val(+$input.val() + operation);
}, 10);
});
<table>
<tbody>
<tr>
<td><input type="text" name="foo[]" value="0"></td>
<td><input type="button" value="+"><input type="button" value="-"></td>
</tr>
<tr>
<td><input type="text" name="foo[]" value="0"></td>
<td><input type="button" value="+"><input type="button" value="-"></td>
</tr>
<tr>
<td><input type="text" name="foo[]" value="0"></td>
<td><input type="button" value="+"><input type="button" value="-"></td>
</tr>
<tr>
<td><input type="text" name="foo[]" value="0"></td>
<td><input type="button" value="+"><input type="button" value="-"></td>
</tr>
<tr>
<td><input type="text" name="foo[]" value="0"></td>
<td><input type="button" value="+"><input type="button" value="-"></td>
</tr>
</tbody>
</table>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>