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

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

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 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