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

170
Views
How to double click on radiobutton to uncheck instead of one click

I'm using 2 groups of radio buttons lets call it 1.1 1.2 and 2.1 2.1 (one radio button of the second group is always checked, depending on which one of the first group is checked, the other one is hidden).

I can't understand why I need to make a double click on the radio button to uncheck it when both radio buttons are checked. I want to click just one time to "uncheck" it.

function show() {
  swich();
}

function swich() {
  $('input[type="radio"]').change(function(){
    $('[data-group="' + $(this).data('group') + '"]').prop('checked', this.checked);
  });
}

var checkedradio = false;

var radioState = [];

$("input[type=radio]").on('click', function(e) {

  if (radioState[this.name] === this) {
      this.checked = false;
      radioState[this.name] = null;
  } else {
      radioState[this.name] = this;
  }
});
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<input data-group="A" id="radio1" required type="radio" value="Yes" name="group1">

<input data-group="B" id="radio2" required type="radio" value="No" name="group1">

<div id="someId1">
  <input data-group="A" id="radio3" type="radio" name="group2" value="Yes" onclick="show()">
</div>

<div id="someId2">
  <input data-group="B" id="radio4" type="radio" name="group2" value="No" onclick="show()">
</div>

about 4 years ago · Juan Pablo Isaza
2 answers
Answer question

0


CAVEAT

We do not recommend you take this approach as it breaks the standard UX for radio buttons.


Your issue is because you re-use checkedradio for both checks.

So:

  • click group A.1 - sets checkedradio = A.1
  • click group A.1 again, works ok and unchecks
  • click group A.1 - sets checkedradio = A.1
  • click group B.1 - sets checkedradio = B.1
  • click group A.1 (checked) - it's not A.1, so doesn't appear to work, set checkedradio = A.1
  • click group A.1 2nd time, works ok

You need a different variable for each group.

As multiple variables become very messy very quickly (and lots of DRY), you can use an array:

var radioState = [];

$(":radio").on('click', function(e) {
  //console.log(radioState[this.name])
  if (radioState[this.name] === this) {
    this.checked = false;
    radioState[this.name] = null;
  } else {
    radioState[this.name] = this;
  }
});
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<input required type="radio" value="Yes" name="group1">
<input required type="radio" value="No" name="group1">
<hr/>
<input type="radio" name="group2" value="Yes">
<input type="radio" name="group2" value="No">

Code based on this answer expanded to use an array.

about 4 years ago · Juan Pablo Isaza Report

0

I was able to achieve the desired result based on this code.

function show() {
  swich();
}

function swich() {
  $('input[type="radio"]').change(function(){
    $('[data-group="' + $(this).data('group') + '"]').prop('checked', this.checked);
  });
}

function toggleRadio(event)
{
    if(event.target.type === 'radio' && event.target.checked === true)
    {
        setTimeout(()=>{ event.target.checked = false; },0);
    }
}
document.addEventListener('mouseup', toggleRadio);
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<input data-group="A" id="radio1" required type="radio" value="Yes" name="group1">

<input data-group="B" id="radio2" required type="radio" value="No" name="group1">

<div id="someId1">
  <input data-group="A" id="radio3" type="radio" name="group2" value="Yes" onclick="show(this)">
</div>

<div id="someId2">
  <input data-group="B" id="radio4" type="radio" name="group2" value="No" onclick="show(this)">
</div>

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!