It's possibile select one radio without name attribute? [closed]
Asked Answered
S

2

3

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?

Sestos answered 8/2, 2016 at 16:26 Comment(4)
It's not very clear what you are asking but if there is no group then checkbox is probably what you want. Please explain in more detail what you are trying to do.Monodrama
Also why would you not use a name? if used in a form the control must have a name or it won't submitMonodrama
What are you using the name for if it's not for grouping? If you need to distinguish the buttons in a group, you should use the value for it.Trinidadtrinitarian
One reason a person might remove a name attribute is to prevent values from being submitted.Clicker
D
12

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">
Doenitz answered 8/2, 2016 at 16:31 Comment(6)
Why would you ever need a configuration like this? It makes no sense and would never submit if used in a form. Form controls with no name won't be submittedMonodrama
@Monodrama I agree using the name attribute as intended is always a good idea, but I dont always have radios even in a form, or if i do i dont want the for to be submitted regularly, i want it done with ajax, in which case not all inputs even need a name. But for radios I dont get why you wouldnt just use the name attribute.Shroyer
...you should also be listening to the change event rather than the click event.Nimesh
@Monodrama there is no evidence from the question that this will be submitted.Doenitz
@JoshCrozier agreed, I have updated the answer.Doenitz
by the same token the question is so vague that use case is completely unknown. To be honest this whole question smells like an XY problem. Sometimes comments like the one I made are as much helpful for the OP as for the answerer. OP may have completely overlooked name issue for exampleMonodrama
S
1

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' />
Shroyer answered 8/2, 2016 at 16:36 Comment(1)
This can use classes or ids or you could put them in a div and and do something like bindRadios("div#div1 input[type=radio]") to get all in that div/group.Shroyer

© 2022 - 2024 — McMap. All rights reserved.