Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

340
Vistas
How can i set unique ID in table? JS

This is the code that i have.(not mine from internet) Now i want to add some option that when cell is created, i want to add some unique ID to each cell, and then when clicking on some cell i will be able to see that ID. how can i do this?

<!DOCTYPE html>
<html>

<head>
    <title>Chess board</title>
    <style>
        body {
            text-align: center;
        }

        .cell {
            height: 30px;
            width: 30px;
            border: 1.5px solid grey;
            border-style: inset;
        }

        .blackcell {
            background-color: black;
        }

        .whitecell {
            background-color: white;
        }
    </style>
</head>
<body>
    <script type="text/javascript">
        var center = document.createElement('center');
        var ChessTable = document.createElement('table');
        for (var i = 0; i < 8; i++) {
            var tr = document.createElement('tr');
            for (var j = 0; j < 8; j++) {
                var td = document.createElement('td');
                if ((i + j) % 2 == 0) {
                    td.setAttribute('class', 'cell whitecell');
                    tr.appendChild(td);
                }

                else {
                    td.setAttribute('class', 'cell blackcell');
                    tr.appendChild(td);
                }
            }
            ChessTable.appendChild(tr);
        }
        center.appendChild(ChessTable);
        ChessTable.setAttribute('cellspacing', '0');
        ChessTable.setAttribute('width', '270px');
        document.body.appendChild(center);
    </script>
</body>

</html>
about 4 years ago · Juan Pablo Isaza
1 Respuestas
Responde la pregunta

0

var center = document.createElement('center');
var ChessTable = document.createElement('table');
for (var i = 0; i < 8; i++) {
    var tr = document.createElement('tr');
    for (var j = 0; j < 8; j++) {
        var td = document.createElement('td');
        td.setAttribute('id','cell'+i+'_'+j)
        td.addEventListener('click',e=>console.log(e.target.id))
        if ((i + j) % 2 == 0) {
            td.setAttribute('class', 'cell whitecell');
            tr.appendChild(td);
        } else {
            td.setAttribute('class', 'cell blackcell');
            tr.appendChild(td);
        }
    }
    ChessTable.appendChild(tr);
}
center.appendChild(ChessTable);
ChessTable.setAttribute('cellspacing', '0');
ChessTable.setAttribute('width', '270px');
document.body.appendChild(center);
<!DOCTYPE html>
<html>

<head>
    <title>Chess board</title>
    <style>
        body {
            text-align: center;
        }

        .cell {
            height: 30px;
            width: 30px;
            border: 1.5px solid grey;
            border-style: inset;
        }

        .blackcell {
            background-color: black;
        }

        .whitecell {
            background-color: white;
        }
    </style>
</head>
<body>

</body>

</html>

about 4 years ago · Juan Pablo Isaza Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda