0

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).

Git Hub
  • 35
  • 2
  • "_When one is checked, and it was clicked while being checked_" what are you trying to say? – Zuckerberg Apr 15 '20 at 07:44
  • @Zuckerberg That radio buttion is currently chekced, and Is being clicked – Git Hub Apr 15 '20 at 07:46
  • 1
    Does this answer your question? [How to make a radio button unchecked by clicking it?](https://stackoverflow.com/questions/10876953/how-to-make-a-radio-button-unchecked-by-clicking-it) – xmaster Apr 15 '20 at 08:09

0 Answers0