javascript - Using jQuery to select/deselect radio buttons with different names -
i have quiz uses ids of answers in db name in order check if they're correct. can't make them same name.
anyways leaves me radio buttons can checked , isn't how should work. found jquery allows radio buttons unchecked need them uncheck when click other buttons.
html:
<fieldset> <input type="radio" name="21"> <label for="21">answer 21</label> <input type="radio" name="22" class="checked"> <label for="22">answer 22</label> <input type="radio" name="23" class=""> <label for="23">answer 23</label> <input type="radio" name="24"> <label for="24">answer 24</label> </fieldset>
script:
$("input[type='radio']").click(function (event) { // if button selected. if ($(this).hasclass("checked")) { // remove placeholder. $(this).removeclass("checked"); // , remove selection. $(this).removeattr("checked"); // if button not selected. } else { // remove placeholder other buttons. $("input[type='radio']").each(function () { $(this).removeclass("checked"); }); // , add placeholder button. $(this).addclass("checked"); } });
so if 21 checked , click 22, 21 should uncheck
if want group of radios inside same fieldset act radio group can do
$("input[type='radio']").click(function (event) { $(this).addclass("checked"); $(this).siblings('input[type="radio"]').prop('checked', false).removeclass('checked') });
demo: fiddle