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

114
Visualizações
Retrieve checkbox label text using javascript

I am using the following javascript function to check all of the checkboxes in a checkbox list:

function processCheckbox (item) {
    $("input[name='" + item + "']").each( function () {
        if($(this).prop('id') === 'OPTIONS_2') {
            $(this).prop('checked', false);
        } else {
            $(this).prop('checked', true);
        }
    
    });
}

I need to leave one specific checkbox unchecked. the checkbox ID is OPTIONS_2 and the above function worked fine at first but problems started occurring due to checkbox list being dynamic and so another option gotten switched to the position of OPTIONS_2.

I tried using $(this).val() in place of $(this).prop('id') but that would not work as the ID comes from the database and changes from environment to environment.

The only option is to use the label text but I am not sure how to get it using javascript.

Here is my html:

<div class="apex-item-option">
    <input type="checkbox" id="OPTIONS_2" name="OPTIONS" value="123">
    <label class="u-checkbox" for="OPTIONS_2">TEST</label>
</div>

Would appreciate any help

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

0

You can use .textContent method to retrieve the label of the checkbox once you reach to label tag with the help of function nextSibling.

$(this).nextSibling.textContent

So let's take your example:

$('#OPTIONS_2').nextSibling.nextSibling.textContent

about 4 years ago · Juan Pablo Isaza Relatório

0

$(this).next().text() === "TEST" worked for me

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