iJade
iJade

Reputation: 23801

unselecting radio input selection

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.

Upvotes: 5

Views: 227

Answers (7)

nils
nils

Reputation: 1668

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

Upvotes: 2

CJ Ramki
CJ Ramki

Reputation: 2630

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

Upvotes: 4

Ankit Tyagi
Ankit Tyagi

Reputation: 2375

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>

Upvotes: 1

Vijayakumar
Vijayakumar

Reputation: 303

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.

Upvotes: 1

java.mypassion
java.mypassion

Reputation: 95

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;
      }
   });

}

Upvotes: 1

Bhojendra Rauniyar
Bhojendra Rauniyar

Reputation: 85545

Use like this:

function check() {
    //logic to check for duplicate selection
    var checked = true ? false : true;
    $(this).prop('checked', checked);
    return false;
}

Upvotes: 1

CREEATION
CREEATION

Reputation: 332

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

Upvotes: 1

Related Questions