jquery - Select specific radio buttons in all groups with no selected checkboxes -
i need to:
- get radio groups no option selected
- from groups inputs "data-correctanswer"
is there better way of doing 1 have? (it's working)
(need select a , h only)
html:
<form class="multiform truefalse"> <ul> <li> <input data-correctanswer="correct" name="question1" type="radio"><span>a</span> </li> <li> <input name="question1" type="radio"><span>b</span> </li> </ul> <ul> <li> <input name="question2" type="radio" checked="checked"><span>c</span> </li> <li> <input data-correctanswer="correct" name="question2" type="radio"><span>d</span> </li> </ul> <ul> <li> <input name="question3" type="radio"><span>e</span> </li> <li> <input data-correctanswer="correct" name="question3" type="radio" checked="checked"><span>f</span> </li> </ul> <ul> <li> <input name="question4" type="radio"><span>g</span> </li> <li> <input data-correctanswer="correct" name="question4" type="radio"><span>h</span> </li> </ul> </form>
js:
$(document).ready(function () { var radio_buttons = $('.multiform input[name*="question"]').not(':checked'); var arr = []; $('.multiform ul').each(function () { notselectedradiogroup = $(this).find('input[name*="question"]'); if (notselectedradiogroup.filter(':checked').length == 0) { arr.push($(this)); } }); $(arr).each(function () { $(this).find('input:radio[data-correctanswer]').not(':checked').addclass('selected'); }); });
try,
$(document).ready(function () { var arr = []; $('.multiform ul').each(function () { var x = $(":radio", this); var cond = x.filter(":not(:checked)").length === x.length; if(cond) arr.push(x.filter("[data-correctanswer]").data("correctanswer")); }); alert(json.stringify(arr)); });
demo
$(document).ready(function () { $('.multiform ul').each(function () { var x = $(":radio", this); var cond = x.filter(":not(:checked)").length === x.length; if (cond) x.filter("[data-correctanswer]").closest("li").css("color","red"); }); });
Comments
Post a Comment