I have 2 radio buttons. This is the behavior I want:
- Initially, they are both set to unchecked
- When one is checked, and the other was clicked, just use the original behavior (switch the checked to unchecked, and the unchecked to checked)
- When one is checked, and it was clicked while being checked -> uncheck it (and keep the other also unchecked)
This is my HTML:
<div id="postDealFormCurrencyContainer" class="postDealFormCurrencyContainer">
<label class="postDealFormCurrencyText postDealFormCurrencyText--borderleft" for="postDealFormShekelsCurrency">₪</label>
<input type="radio" id="postDealFormShekelsCurrency" name="post_deal[currency]" value="shekels" style="display: none;">
<label class="postDealFormCurrencyText" for="postDealFormDollarsCurrency">$</label>
<input type="radio" id="postDealFormDollarsCurrency" name="post_deal[currency]" value="dollars" style="display: none;">
</div>
This is the jQuery:
let $postDealFormCurrencyContainerOBJ = $('#postDealFormCurrencyContainer');
$postDealFormCurrencyContainerOBJ.on('click', 'label', function() {
if($(this).hasClass('postDealFormCurrencyText--selected') === true) {
$(this).removeClass('postDealFormCurrencyText--selected').next().prop('checked', false);
} else {
$(this).addClass('postDealFormCurrencyText--selected');
$(this).siblings('label').removeClass('postDealFormCurrencyText--selected');
}
});
This is doing the job for the first two terms I've pointed above, but the third one is not satistified by this jQuery code. This is because I did uncheck the checked clicked radio button, but right after it automatically checks it back (because I did an event listener to the label
and right after the default event listener of input
is called because it was also clicked - then it checks it back).