¿Cómo puedo cambiar el html interno obteniendo elemento por índice?
quiero cambiar el contenido de las celdas de acuerdo con sus valores de índice
<table> <tr><td></td><td></td></tr> <tr><td></td><td></td></tr> <tr><td></td><td></td></tr> </table> function LFLS() { // LFLS => load from local Storage for (i = 0; i < localStorage.length; i++) { key = localStorage.key(i);//it return values like ("1,2","2,5", etc.) console.log(key) row = key.split(",")[0]; col = key.split(",")[1]; //how to get the cell by row and col } }Como dijo Sakil, puedes usar eq(). Prueba esto:
function LFLS() { // load from local Storage for (i = 0; i < localStorage.length; i++) { key = localStorage.key(i); row = key.split(",")[0]; col = key.split(",")[1]; // how to get the cell by row and col $("table tr").eq(row).children().eq(col).html('NEW VALUE') } }Creo que necesitas los siguientes fragmentos.
Antes de tu bucle for
const rows = $("table tr");Después de obtener variables de fila y columna
const cellToUpdate = rows[row].children[col];Alternativamente, si está buscando hacer un bucle programático a través de la tabla, puede usar el siguiente fragmento.
<script type="text/javascript"> const rows = $("table tr"); for( i = 0; i < rows.length; i++ ) { const currRow = rows[i]; const rowChildren = currRow.children; for( n = 0; n < rowChildren.length; n++ ) { const cell = rowChildren[n]; cell.innerHTML = "My new data for row: " + i + " in cell " + n; } } </script>¿Algo en las líneas de abajo no va a funcionar para usted por alguna razón?
$("table tr:nth-of-type([your-row])).eq([your-col]).html([your_content]);