Joe Peteny February 2016

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:

<input type="checkbox" name="radio">
<input type="checkbox" name="radio">

Anyone can help me to choice another method to render the radio with exclusive choice without name attribute?

Answers


BenG February 2016

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">


Dustin Poissant February 2016

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 Status

Asked in February 2016
Viewed 1,304 times
Voted 8
Answered 2 times

Search




Leave an answer