5

In a part of my application where i check for duplicate radio input selection and revert if its already selected to early selection.

Here is my html code ..

<input type="radio" name="A" checked="checked" onclick="return check();" />
<input type="radio" name="A" onclick="return check();" />

<br />

<input type="radio" name="B"  onclick="return check();" />
<input type="radio" name="B" checked="checked" onclick="return check();" />

Here is the javascript code

function check() {
    //logic to check for duplicate selection
    alert('Its already selected');
    return false;
}

And here is the demo The above code works fine. The issue is when the input isn't initially checked. In such condition the radio input selection doesn't revert to unchecked.

NOTE: when in checked state, returning false shows and alert and sets the check box to initial checked state. But when initially in non checked state this doesn't work.

iJade
  • 23,144
  • 56
  • 154
  • 243

7 Answers7

4

In DOM ready, check if any radio button is checked or not. If any radio button is checked, increase the counter by one. In onclick of the radio button, check if the counter value is 1. if yes, return false, else increase counter by 1.

try this code,

html

<input type="radio" name="A" checked="checked" />
<input type="radio" name="A" />
<br />
<input type="radio" name="B" />
<input type="radio" name="B" />

JS

var counterA = 0;
var counterB = 0;
$(document).ready(function () {
    if ($("input:radio[name=A]").is(":checked") == true) counterA++;
    if ($("input:radio[name='B']").is(":checked") == true) counterB++;
});

$('input:radio[name=A]').click(function () {
    if (counterA == 1) {
        alert('already checked');
        return false;
    } else {
        counterA++;
    }
});
$('input:radio[name=B]').click(function () {
    if (counterB == 1) {
        alert('already checked');
        return false;
    } else {
        counterB++;
    }
});

SEE THIS DEMO

CJ Ramki
  • 2,620
  • 3
  • 25
  • 47
  • this does not work for the case iJay was asking for: if one radio is not initially selected it fails: http://jsfiddle.net/GSe56/2/ – nils Mar 18 '14 at 11:21
  • This solution does not scale when you have more categories. You would need to update the code each time a category is added – nils Mar 19 '14 at 06:53
  • I asked iJay to clearify the scenario and updated my answer accordingly. – nils Mar 19 '14 at 08:43
2

iJay wants to ask several questions and privides the same answers for each question. Each answer can only be choosen once. If a user clicks the same answer the second time a error-message should be shown.

// get all elements
var elements = document.querySelectorAll('input[type="radio"]');

/**
 * check if radio with own name is already selected
 * if so return false
 */
function check(){

  var selected_name = this.name,
      selected_value = this.value,
      is_valid = true;

  // compare with all other elements
  for(var j = 0; j < len; j++) {
      var el = elements[j];

      // does the elemenet have the same name AND is already selected?
      if(el.name != selected_name && el.value == selected_value && el.checked){
        // if so, selection is not valid anymore
          alert('Oups..! You can not select this answer a second time :( Choose another one!')

        // check current group for previous selection
        is_valid = false;
        break;
      }
  };

  return is_valid;
}

/**
 * bind your elements to the check-routine
 */
for(var i = 0, len = elements.length; i < len; i++) {
    elements[i].onmousedown = check;
}

Here is a DEMO

nils
  • 1,668
  • 14
  • 15
1

Use $yourRadio.prop('checked', false); to uncheck the specific radio.

CREEATION
  • 232
  • 1
  • 10
1

Use like this:

function check() {
    //logic to check for duplicate selection
    var checked = true ? false : true;
    $(this).prop('checked', checked);
    return false;
}
Bhojendra Rauniyar
  • 83,432
  • 35
  • 168
  • 231
1

1) add class attribute to same type of checkbox elements(which are having same name)

   ex: class = "partyA"

2)
var sourceIdsArr = new Array();

function check() {
     $('.partyA').each(function() {
 var sourceId = $(this).val();
      if(sourceIdsArr.indexOf(sourceId) != -1){
         sourceIdsArr.push(sourceId );
      }
      else{
         alert('Its already selected');
         return false;
      }
   });

}
1

Here is your code..

function check() {
        //logic to check for duplicate selection
 var selectflag=0;

 var radiovalue=document.getElementsByName("B");
 for(var i=0;i<radiovalue.length;i++)
 {
    // alert(radiovalue[i].checked);
     if(radiovalue[i].checked==true)
     {
         selectflag=1;
         break;
     }
 }
 if(selectflag==1)
 {
        alert('Its already selected');
        return false;
 }
 return true;
  }

Trigger your event on MouseDown. It will work fine.

Vijayakumar
  • 303
  • 4
  • 10
1

I think this is something you are looking for :

<html>
<head>
<script src="http://code.jquery.com/jquery-1.11.0.min.js"></script>
</head>
<body>

 <input type="radio" name="A" checked="checked"  onclick="return check(this);"/>
 <input type="radio" name="A" onclick="return check(this);"/>
<script>
$( document ).ready(function() {
  this.currentradio = $("input[name='A']:checked")[0];
});
function check(t) {
     var newradio= $("input[name='A']:checked")[0]; 
    if (newradio===document.currentradio){
    alert('already selected');
        return false
    }else{
    document.currentradio = $("input[name='A']:checked")[0];
    }
 }
</script>
</body>
<html>
Ankit Tyagi
  • 2,381
  • 10
  • 19