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

181
Vistas
How to add the same class for all the td using javaScript

Hi everyone I am new to js. How to add the same class for all the td using javaScript

<!doctype html>
<html lang="en">
<head>
  <meta charset="utf-8"/>
  <title>Tic Tac Toe</title>
  <link rel="stylesheet" href="style.css"/>
  <script src="script.js" defer></script>
</head>
<body>  
  <h1>Tic Tac Toe</h1>
  <table>
    <tr>
      <td id="c0"></td>
      <td id="c1"></td>
      <td id="c2"></td>
    </tr>
    <tr>
      <td id="c3"></td>
      <td id="c4"></td>
      <td id="c5"></td>
    </tr>
    <tr>
      <td id="c6"></td>
      <td id="c7"></td>
      <td id="c8"></td>
    </tr>
  </table>
</body>
</html>

I have tried it like this but i am getting this erorr Uncaught TypeError: all.setAttribute is not a function at script.js:9 js

const  all = document.querySelectorAll('td');
all.setAttribute('class', 'tds');
about 4 years ago · Juan Pablo Isaza
2 Respuestas
Responde la pregunta

0

You are using querySelectorAll here, So you will get array like elements of all HTML td element

You just have to loop over and add class to it

const all = document.querySelectorAll('td');
all.forEach((item, i) => {
  item.setAttribute('class', 'tds');
});

You can also use

item.classList.add("tds");

Note: For demo purpose, I've used added text in between td and some CSS.

const all = document.querySelectorAll('td');
all.forEach((item, i) => {
  item.setAttribute('class', 'tds');
});
.tds{
  background-color: lime;
}
<h1>Tic Tac Toe</h1>
<table>
  <tr>
    <td id="c0">c0</td>
    <td id="c1">c1</td>
    <td id="c2">c2</td>
  </tr>
  <tr>
    <td id="c3">c3</td>
    <td id="c4">c4</td>
    <td id="c5">c5</td>
  </tr>
  <tr>
    <td id="c6">c6</td>
    <td id="c7">c7</td>
    <td id="c8">c8</td>
  </tr>
</table>

about 4 years ago · Juan Pablo Isaza Denunciar

0

It's very easy using jquery, it can work with a single line of code as seen below

$("td").attr("class", "tds");
/*To show that it works, i added a css code to change the background color so you know it really added the class to the td*/

.tds{
  background-color: green;
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<h1>Tic Tac Toe</h1>
<table>
  <tr>
    <td id="c0">c0</td>
    <td id="c1">c1</td>
    <td id="c2">c2</td>
  </tr>
  <tr>
    <td id="c3">c3</td>
    <td id="c4">c4</td>
    <td id="c5">c5</td>
  </tr>
  <tr>
    <td id="c6">c6</td>
    <td id="c7">c7</td>
    <td id="c8">c8</td>
  </tr>
</table>

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