Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

113
Vistas
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 Respuestas
Responde la pregunta

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 Denunciar

0

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

about 4 years ago · Juan Pablo Isaza Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda