Mi pregunta es: ¿Cómo puedo hacer que una celda de tabla, por ejemplo, cambie el texto al hacer clic? Intenté esto:
function randomFunction() { document.getElementById("randomId").innerText = "random text" } <table> <tr> <td id="randomId" onclick="randomFunction()">random text</td> </tr> </table> Pero hace el atributo onclick para toda la fila.
¿Es posible activar la función solo cuando hago clic en la celda?
Sí es posible. tienes que pasar el contexto a la función. Puse más celdas en tu tabla.
function randomFunction(e){ e.innerText = "HuhU" // document.getElementById("randomId").innerText = "random text!" } td { background: green; color:white; } <table> <tr> <td id="randomId" onclick="randomFunction(this)">random text</td> <td id="randomId" onclick="randomFunction(this)">random text</td> <td id="randomId" onclick="randomFunction(this)">random text</td> </tr> </table>