Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

112
Views
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 answers
Answer question

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 Report

0

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

about 4 years ago · Juan Pablo Isaza Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!