html - make checkbox behave like radio buttons with javascript -
i need manipulate behavior of check boxes javascript. should behave radio buttons (only 1 selectable @ time, plus unselect previous selections).
the problem can't use plain radio buttons in first place, because name attribute each radio button different.
i know not ultimate , shiniest solutions make apple pear, , w3c wouldn't give me thumbs it, better solution right change core php logic of entire cms structure ;-)
any appreciated!
html :
<label><input type="checkbox" name="cb1" class="chb" /> checkbox1</label> <label><input type="checkbox" name="cb2" class="chb" /> checkbox2</label> <label><input type="checkbox" name="cb3" class="chb" /> checkbox3</label> <label><input type="checkbox" name="cb4" class="chb" /> checkbox4</label>
jquery :
$(".chb").change(function() { $(".chb").prop('checked', false); $(this).prop('checked', true); });
if want user can unchecked selected item :
$(".chb").change(function() { var checked = $(this).is(':checked'); $(".chb").prop('checked',false); if(checked) { $(this).prop('checked',true); } });
demo :
Comments
Post a Comment