Skip to content Skip to sidebar Skip to footer

It's Possibile Select One Radio Without Name Attribute?

I need to use attribute name so I can use 'name' attribute for radio button. I know that the code for radio button with exclusive choice is:

Solution 1:

you could use a class:-

$('.radio').change(function() {
  $('.radio').not(this).prop('checked', false);
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>

<input type="radio" class="radio">
<input type="radio" class="radio">

or even just by the type:-

var radios = $('[type="radio"]');

radios.change(function() {
  radios.not(this).prop('checked', false);
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>

<input type="radio">
<input type="radio">

Solution 2:

I suggest something similar to @BG101 but wrap it in a function and allow any jQuery selector to be used so you could target multiple groups.

https://jsfiddle.net/53knnzho/

function bindRadios(selector){
  $(selector).click(function() {
    $(selector).not(this).prop('checked', false);
  });
};

bindRadios("#radio1, #radio2, #radio3");
bindRadios("#radio4, #radio5, #radio6");
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<input type='radio' id='radio1' />
<input type='radio' id='radio2' />
<input type='radio' id='radio3' />
<br><br>
<input type='radio' id='radio4' />
<input type='radio' id='radio5' />
<input type='radio' id='radio6' />

Post a Comment for "It's Possibile Select One Radio Without Name Attribute?"