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

184
Vistas
Uncaught TypeError: Cannot read properties of undefined (reading 'type')

I have a table with two columns: values and checkboxes. I want to identify the smallest value with a checked checkbox. I adapted the setup from this question, but changed the way I identify checkboxes from document.getElementsByTagName('input') to querySelectorAll(".myCheckbox"). The initial way became a problem when I added an additional input field (which is why I included it below)

However, the function throws Uncaught TypeError: Cannot read properties of undefined (reading 'type').

What's going wrong?

function smallestWeight() {
  let values = [];
  const ele = document.querySelectorAll(".myCheckbox");
  let table = document.getElementById("myTable");
  let trs = table.getElementsByTagName("tr");
  for (i = 0, len = trs.length; i < len; i++) {
    if (ele[i].type == 'checkbox' && ele[i].checked == true) {
      values.push(parseFloat(trs[i].cells[0].innerHTML));
    }
  }
}


const btn = document.getElementById("click");
btn.addEventListener("click", function() {
  smallestWeight();
})
<table id="myTable">
  <thead>
    <tr>
      <th>value</th>
      <th>include</th>
    </tr>
  </thead>
  <tbody>
    <tr>
      <td>1.1</td>
      <td><input type="checkbox" class="myCheckbox" id="include" value="include" checked></td>
    </tr>
    <tr>
      <td>2.2</td>
      <td><input type="checkbox" class="myCheckbox" id="include" value="include" checked></td>
    </tr>
    <tr>
      <td>3.3</td>
      <td><input type="checkbox" class="myCheckbox" id="include" value="include" checked></td>
    </tr>
    <tr>
      <td>4.4</td>
      <td><input type="checkbox" class="myCheckbox" id="include" value="include" checked></td>
    </tr>
  </tbody>
</table>

<button id="click">Click</button><br>
<input type="number">

about 4 years ago · Juan Pablo Isaza
1 Respuestas
Responde la pregunta

0

Your code looks at all rows in the table. You do not take into account that the first row of the table is the thead. So your table rows is one more greater than the checkboxes.

const trs = table.querySelectorAll("tbody tr");
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