Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

218
Visualizações
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 Respostas
Responde à pergunta

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 Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda