I have followed this StackOverflow Question's Answer1 and Answer2
but not getting result.. As I am calling all checkbox list's data from Range
tabel. and their id is also defined by data.. Let me show you the code for this screenshot
= f.collection_check_boxes :range_ids, Range.active, :id, :name, {},:class=>'checkbox'
which returns as
<input id="campaign_range_ids_1" class="checkbox" type="checkbox" value="1" name="campaign[range_ids][]">
<input id="campaign_range_ids_2" class="checkbox" type="checkbox" value="2" name="campaign[range_ids][]">
<input id="campaign_range_ids_3" class="checkbox" type="checkbox" value="3" name="campaign[range_ids][]">
<input id="campaign_range_ids_4" class="checkbox" type="checkbox" value="4" name="campaign[range_ids][]">
I want if All
means id="campaign_range_ids_4"
is selected/deselected then all other check-boxes should be perform accordingly..
Here also have a screenshot of Range Table
I had tried this Javascript
:javascript
$('#campaign_range_ids_4').click(function() {
if(this.checked) {
$('#campaign_range_ids_1').checked = true;
$('#campaign_range_ids_2').checked = true;
$('#campaign_range_ids_3').checked = true;
});
} else {
$('#campaign_range_ids_1').checked = false;
$('#campaign_range_ids_2').checked = false;
$('#campaign_range_ids_3').checked = false;
});
}
});
Please correct me where I do Mistake.... Thanks in advance.. :)
Updated Question
I have followed Shridhar's and Pavan's answer it works perfectly but now I want that when I select All then deselect any one then it must uncheck "All" (4th checkbox) but it remains as it is..
Modifying Sridhar's answer finally I got the Answer of my Updated Question's.
I wanted if
All
(4th checkbox) ischecked
then all check-box should be selected and if it'sunchecked
then all check-box should beunchecked
it works perfectlyBut Issue I had found that If
All
(4th checkbox) is selected then it check all boxes then if I unchecked any check-box thenAll
(4th checkbox) must beuncheked
which was not occur.Solution by Modifying Code: In future if other people face the same issue..
HTML Code:
JavaScript:
Working Demo:
change the selector to
$('#campaign_range_ids_4')
and do this:instead of
click
usechange
method.Error in Selectors ,It should be
$('#campaign_range_ids_4')
not$('#campaign_beacon_range_ids_4')
useprop()
to set the checkbox stateTry this
DEMO
OR
DEMO
As i said,it should be
$('#campaign_range_ids_4')
not$('#campaign_beacon_range_ids_4')
This will work too
Demo