Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

216
Vistas
How do I make an event Listener (onclick) for a single Table Cell

My question is: How can I make a table cell, for example, change text on click? I tried this:

function randomFunction() {
  document.getElementById("randomId").innerText = "random text"
}
<table>
  <tr>
    <td id="randomId" onclick="randomFunction()">random text</td>
  </tr>
</table>

But it does the onclick attribute for the entire row.

Is it possible to trigger the function only when I click on the cell ?

about 4 years ago · Juan Pablo Isaza
1 Respuestas
Responde la pregunta

0

Yes it possible. you have pass the context to the function. I put more cells in your table.

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>

about 4 years ago · Juan Pablo Isaza Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda