Monday, May 20, 2024
 Popular · Latest · Hot · Upcoming
72
rated 0 times [  73] [ 1]  / answers: 1 / hits: 89624  / 13 Years ago, sat, april 30, 2011, 12:00:00

I need to manipulate the behavior of the check boxes with javascript. They should basically behave like radio buttons (only one selectable at a time, plus unselect any previous selections).



The problem is that I can't use plain radio buttons in first place, because the name attribute for each radio button would be different.



I know its not the ultimate and shiniest solutions to make an apple look like a pear, and w3c wouldn't give me their thumbs for it, but it would be a better solution right now than to change the core php logic of the entire cms structure ;-)



Any help is much appreciated!


More From » html

 Answers
3

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 you want user can unchecked selected item :



$(.chb).change(function() {
$(.chb).not(this).prop('checked', false);
});


Demo :



http://jsfiddle.net/44Zfv/724/


[#92471] Thursday, April 28, 2011, 13 Years  [reply] [flag answer]
Only authorized users can answer the question. Please sign in first, or register a free account.
emmaleet

Total Points: 203
Total Questions: 107
Total Answers: 98

Location: Cook Islands
Member since Thu, May 21, 2020
4 Years ago
;