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

261
Visualizações
HTML Table - Loop through dynamically created rows and if field in specific column contains date prevent row from editing

I'm trying to create a JavaScript to disable a row if EndDate contains a date. The table is generate automatically so I thought to lookup how many rows the table has and start iterating through them. Check if fields in column EndDate contain a date. If that row contains a date then disable any field in that row.

To track down the fields I used this to check if I could disable them.

document.getElementById("r0_LineItemCode").disabled = "true";
document.getElementById("r0_LineItemCode_ref_ref").disabled = "true";
document.getElementById("pr0_LineItemCode").disabled = "true";
document.getElementById("r0_ec_type").disabled = "true";

Table

Table code

DateField

I accomplished to loop through the rows and find out how many rows there are and show the data on the console. But now selecting that field to check if it contains a date and then disable those row items fails.

Code to track down the rows and iterate through the rows:

let text = "";
var endDateFields = [];
var rowData  = document.querySelector("#RequestLinesGrid_RowIDs");
console.log(rowData);
console.log(rowData.value);
const rows = rowData.value.split(',');

rows.forEach(myFunction);

console.log(text);
console.log(r0_EndDate);
console.log(r1_EndDate);
console.log(r2_EndDate);

function myFunction(item, index) {
  text += "indexNumber = " + index + " variableField: " + item + "_EndDate";

        var str = item + "_EndDate";
        eval(str);
        console.log(str);

}

I have the feeling I'm almost there, but need a little push in the right direction :)

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

0

This is a simple logic of your problem, if date is not empty disable all inputs of the parent tr.

document.querySelector('table').querySelectorAll('td.date').forEach(el => {
  if (el.innerHTML !== '') {
    const parentTr = el.parentElement;
    parentTr.querySelectorAll('input').forEach(input => {
      input.disabled = true;
    });
  }
});
<table>
  <thead>
    <th>id</th>
    <th>input1 Example</th>
    <th>input2 Example</th>
    <th>date Example</th>
  </thead>
  <tbody>
    <tr>
      <td>1</td>
      <td><input name='test' type='text'></td>
      <td><input name='test' type='text'></td>
      <td class='date'>10/10/2021</td>
    </tr>
    <tr>
      <td>2</td>
      <td><input name='test' type='text'></td>
      <td><input name='test' type='text'></td>
      <td class='date'>10/10/2021</td>
    </tr>
    <tr>
      <td>1</td>
      <td><input name='test' type='text'></td>
      <td><input name='test' type='text'></td>
      <td class='date'></td>
    </tr>
  </tbody>
</table>

Instead of check all row just add a simple class on td have date then check it as my example.

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