I have many checkboxes each having the same ID’s and Classes.
The input’s themselves have the class ‘el-checkbox__original’.
<!-- when this is clicked the below divs which have checkboxes appear -->
<div id="bap-service-13" class="bpa-front-si-card">
<div class="bpa-front-si-card__row">
</div>
</div>
<div id="treatment-option-ext" class="bpa-service-extra__item">
<div class="bpa-sei__header">
<div class="bpa-sei__left">
<div class="bpa-sei__left-checkbox">
<label class="el-checkbox bpa-form-label bpa-custom-checkbox--is-label bpa-front-form-control--checkbox">
<span class="el-checkbox__input">
<span class="el-checkbox__inner"></span>
<input type="checkbox" aria-hidden="false" class="el-checkbox__original" value="">
</span>
</label>
</div>
<div class="bpa-sei__left-body">
<div class="bpa-se--heading">1.1ml</div>
<div class="bpa-se--options">
<div class="bpa-se-o__item">£0.00</div>
</div>
</div>
</div>
<div class="bpa-sei__right">
</div>
</div>
</div>
<div id="treatment-option-ext" class="bpa-service-extra__item">
<div class="bpa-sei__header">
<div class="bpa-sei__left">
<div class="bpa-sei__left-checkbox">
<label class="el-checkbox bpa-form-label bpa-custom-checkbox--is-label bpa-front-form-control--checkbox">
<span class="el-checkbox__input">
<span class="el-checkbox__inner"></span>
<input type="checkbox" aria-hidden="false" class="el-checkbox__original" value="">
</span>
</label>
</div>
<div class="bpa-sei__left-body">
<div class="bpa-se--heading">1.1ml</div>
<div class="bpa-se--options">
<div class="bpa-se-o__item">£0.00</div>
</div>
</div>
</div>
<div class="bpa-sei__right">
</div>
</div>
</div>
<div id="treatment-option-ext" class="bpa-service-extra__item">
<div class="bpa-sei__header">
<div class="bpa-sei__left">
<div class="bpa-sei__left-checkbox">
<label class="el-checkbox bpa-form-label bpa-custom-checkbox--is-label bpa-front-form-control--checkbox">
<span class="el-checkbox__input">
<span class="el-checkbox__inner"></span>
<input type="checkbox" aria-hidden="false" class="el-checkbox__original" value="">
</span>
</label>
</div>
<div class="bpa-sei__left-body">
<div class="bpa-se--heading">1.1ml</div>
<div class="bpa-se--options">
<div class="bpa-se-o__item">£0.00</div>
</div>
</div>
</div>
<div class="bpa-sei__right">
</div>
</div>
</div>
When checked, class is-checked is added to both the span tag and label tag
<div id="treatment-option-ext" class="bpa-service-extra__item">
<div class="bpa-sei__header">
<div class="bpa-sei__left">
<div class="bpa-sei__left-checkbox">
<label class="el-checkbox bpa-form-label bpa-custom-checkbox--is-label bpa-front-form-control--checkbox is-checked">
<span class="el-checkbox__input is-checked">
<span class="el-checkbox__inner"></span>
<input type="checkbox" aria-hidden="false" class="el-checkbox__original" value="">
</span>
</label>
</div>
<div class="bpa-sei__left-body">
<div class="bpa-se--heading">1.1ml</div>
<div class="bpa-se--options">
<div class="bpa-se-o__item">£0.00</div>
</div>
</div>
</div>
<div class="bpa-sei__right">
</div>
</div>
</div>
How can I make it so that if one the checkboxes is clicked (el-checkbox__original), any of the other checkboxes are unticked, ultimately so that only one checkbox can be checked.
I tried with JQuery but evidently, it does not remove the is-checked from any of the other checkboxes when one of them is clicked
jQuery.noConflict();
setTimeout(function(){
jQuery(document).ready(function($) {
console.log("page loaded")
$(".bpa-front-si-card").on('click', function(){
console.log("service clicked")
});
<!-- after clicking bpa-front-si-card - line above, checkboxes with class el-checkbox__original appear and should listen for checkboxes to be clicked -->
$(".el-checkbox__original").on('click', function(){
$('input.el-checkbox__original').not(this).prop('checked', false).closest("label").removeClass('is-checked').closest("span").removeClass('is-checked');
})
})}, 6000);
2
Answers
UPDATED (based on OP comment updates)
If you want to deselect all bar the active one, you could uncheck them all then re-check the active on, its a simple way i guess
Just add it inside whatever event creates these elements and you’ll be set
PUT THIS CODE inside your
.bpa-front-si-card.'click'
event if the elements targeted aren’t created untul that event is fired.or acccording to the documentation you could do something like this to watch for live elements
Using a delegated event listener solves the problem: