Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

210
Views
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 answers
Answer question

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 Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!