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

148
Visualizações
How to check element in table and compare value with array

What I want to do: The function I want to do is compare and insert data into the input field, it should run through the firstName column in the table and compare with the firstName field in the array , if any of those is the same then it should print the value to the input field.

Problem: I have no idea how to check every row in the firstName column in the table and compare it to the firstName value in the array.

const student= ["Janet", "Weaver","Adam"];

let text = "";
student.forEach(myFunction);
student.forEach(element => console.log(element));
function myFunction(item) {
  text += item + "<br>"; 
}

//Check the firstName column in table
//if(td = student.firstName){
  //document.getElementById("janet").innerHTML = student.firstName;
//}
<!DOCTYPE html>
  <html>
  <style>
    table,
    th,
    td {
      border: 1px solid black;
    }
  </style>

  <body>

    <h2>User table</h2>
    <table style="width:100%">
      <tr>
        <th>First name</th>
        <th>Age</th>
        <th>Caption</th>
      </tr>
      <tr>
        <td>Janet</td>
        <td>16</td>
        <td><input type="text" id="janet" placeholder="Name"></input>*here should appear the name of Janet*
        </td>
        <tr>
          <td>John</td>
          <td>19</td>
          <td><input type="text" placeholder="Name"></input>
          </td>
        </tr>
    </table>

  </body>

  </html>

about 4 years ago · Juan Pablo Isaza
1 Respostas
Responde à pergunta

0

You should probably run throught your table rows, then compare the first column value with your Array

EDIT : document.getElementsByTagName('tr') returns an HTMLCollection that needs to be converted to an Array to allow you to use forEach

Array.from(document.getElementsByTagName('tr')).forEach(function(row) {

    let firstTD = row.getElementsByTagName('td').item(0);
    if (firstTD === null) { return; }
    let firstname = firstTD.innerHTML; 
    
    // does the array contain firstname?
    if (student.indexOf(firstname)>=0) {
        let input_col = row.getElementsByTagName('td').item(2);
        let input = input_col.getElementsByTagName('input').item(0);
       
        input.value = firstname;
    }

});

Note that it would probably be easier to use IDs or classes instead of counting the tags with getElementsByTagName...

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