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

79
Visualizações
Check a checkbox based on text that follows in a different div?

Is there a way to check checkboxes based on text that follows them? An App at work that requires me to check ALOT of boxes depending on the text that follows. Seems like it could be done programmatically, but it's definitely beyond my limited knowledge

Here's an obscured snippet of the page.

  <tr>
    <td><span title="original"><input id="a0" type="checkbox" name="orginal_a0" value="0"><label for="a0">
          <div class="List">
            <div>
              <div>Default Thing</div>
            </div>
          </div>
        </label></span></td>
  </tr>
  <tr>
    <td><span title="original"><input id="a1" type="checkbox" name="original_a1" value="1"><label for="a1">
          <div class="List">
            <div>
              <div>New Thing</div>
            </div>
          </div>
        </label></span></td>
  </tr

I know I could based on the input id or name, but unfortunately they don't help identify if I should or should not check the box. It's the text that follows the box that's important. If iIcould make it so it would check any box that begin with "New" or any other text, but also not check the ones that begin with "Default". So in the example above I'd check the 2nd box and not check the 1st one. In the app it's generally checking 50-75 boxes but leaving a similar amount unchecked. Ideally it would be something I could just type into the console or make into a bookmarklet.

Is this possible?

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

0

You could loop over all the cells, check if the text doesn't begin with default and set the checked state to the input

As a side note it's better to not nest a div element inside an inline element like a span or a label

const td = document.querySelectorAll('td');
[...td].forEach((cell) => {
  const text = cell.textContent.trim();
  if (!text.match(/^default/i)) {
    cell.querySelector('input').checked = true;
  }
});
<table>
  <tr>
    <td>
      <span title="original">
        <input id="a0" type="checkbox" 
               name="orginal_a0" value="0">
        <label for="a0">Default Thing</label>
      </span>
    </td>
  </tr>
  <tr>
    <td>
       <span title="original">
         <input id="a1" type="checkbox" 
                name="original_a1" value="1">
         <label for="a1">New Thing</label>
       </span>
    </td>
  </tr>
</table>

about 4 years ago · Juan Pablo Isaza Relatório

0

This example loops through all the checkboxes and grabs the next element, which is assumed to be the label. If the div indicated doesn't begin with "Default" it will check the box.

const allCheckboxes = document.querySelectorAll("input[type='checkbox']");

for (let checkbox of allCheckboxes) {
  // get the label element and locate the appropriate div within
  let appropriateDiv = checkbox.nextElementSibling.querySelector("div.List div div");
  if (appropriateDiv && !appropriateDiv.innerText.startsWith("Default")) {
    checkbox.checked = true;
  }
}
<table><tbody>
<tr>
    <td><span title="original"><input id="a0" type="checkbox" name="orginal_a0" value="0"><label for="a0">
          <div class="List">
            <div>
              <div>Default Thing</div>
            </div>
          </div>
        </label></span></td>
  </tr>
  <tr>
    <td><span title="original"><input id="a1" type="checkbox" name="original_a1" value="1"><label for="a1">
          <div class="List">
            <div>
              <div>New Thing</div>
            </div>
          </div>
        </label></span></td>
  </tr>
</tbody></table>

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