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

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

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