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
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
$(this).next().text() === "TEST" worked for me