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

198
Views
Javascript onclick function, how to get it to repeat?
var table = document.querySelectorAll(".validcell")[0];
if (table) table.onclick = function (e) {
    var target = (e || window.event).target;
    if (target.tagName in { TD: 1, TH: 1 })
        target.setAttribute('style', 'background-color: #F00');
    setInterval(table, 50);
};

Trying to get this function to repeat whenever a cell on 10/10 table is clicked. Seems to stop after 1 cell, top left corner is selected. Any ideas?

about 4 years ago · Juan Pablo Isaza
1 answers
Answer question

0

var table = document.querySelectorAll(".validcell");
       
table.forEach(cell => cell.onclick = onclickHandler)

function onclickHandler(e) { 

  var target = e.target;

    if (target.tagName in { TD: 1, TH: 1 })
    {
      target.setAttribute('style', 'background-color: #F00');
    }
    
};
td, th{
  padding:5px;
  border:1px solid black
}
<table>
  <tr>
    <th class="validcell">Header1</th>
    <th class="validcell">Header2</th>
    <th class="validcell">Header3</th>
   <tr>
   <tr>
    <td class="validcell">Dummy Text</td>
    <td class="validcell">Dummy Text</td>
    <td class="validcell">Dummy 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!