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

221
Views
How do I set an on click event for every cell that calls a function which takes id of cell as a parameter in JavaScript

Here I created a table and assigned each cell an id. What I want to do is something like this cell1.onclik = MyFunction(this.id);

function createTable() {
        var table = document.getElementById("tabela2"); 
        for (let j = 0; j < k; j++) {
            var row = table.insertRow(0);
            for (let i = 0; i < l; i++) {
                var cell1 = row.insertCell(0);
                cell1.id = "celica" + i + "_" + j; 
            /*doesnt work: cell1.onclik = MyFunction(this.id); */           
            }
        }
    }
about 4 years ago · Juan Pablo Isaza
1 answers
Answer question

0

Don't use onXXX events. It won't work it there is a content-security-policy on the page.

You just need to listen to the table

    function createTable() {
            var table = document.getElementById("tabela2"); 
//            document.body.appendChild(table);
            for (let j = 0; j < 4; j++) {
                var row = table.insertRow(0);
                for (let i = 0; i < 4; i++) {
                    var cell1 = row.insertCell(0);
                    cell1.textContent = "1";
                    cell1.id = "celica" + i + "_" + j; 
                }
            }
            table.addEventListener("click", function(e) {
              console.log(e.target.id);
              console.log(e.target.tagName);
            });
        }
        
        createTable();
<html><body><table id='tabela2'></table></body></html>

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!