Reputation: 477
I have searched for hours on this and read previous posts, but still no luck. In my code, individual countries tick and highlight nicely, but I can't get the select all/ untick all buttons to do anything.
My expected result is that: select all (CountrySelectAll_ID_ws
) button should tick and highlight all 3 countries. The un-select button (CountrySelectNone_ID_ws
) should remove all ticks and all highlights.
Does anyone have any suggestions?
I have a fiddle: http://jsfiddle.net/petg1bhb/
My code:
<input type="button" id="CountrySelectAll_ID_ws" value ='Tick all' />
<input type="button" id="CountrySelectNone_ID_ws" value ='UNtick' />
<div class="multiselect">
<br>
<label><input type="checkbox" name="option[]" value="1" />Germany</label>
<label><input type="checkbox" name="option[]" value="2" />France</label>
<label><input type="checkbox" name="option[]" value="7" />Spain</label>
</div>
jQuery.fn.multiselect = function() {
$(this).each(function() {
var checkboxes = $(this).find("input:checkbox");
checkboxes.each(function() {
var checkbox = $(this);
// Highlight pre-selected checkboxes
if (checkbox.attr("checked"))
checkbox.parent().addClass("col-on");
// Highlight checkboxes that the user selects
checkbox.click(function() {
if (checkbox.attr("checked"))
checkbox.parent().addClass("col-on");
else
checkbox.parent().removeClass("col-on");
alert$("option:selected");
});
});
});
};
$(function() {
$(".multiselect").multiselect();
});
$(function() {
$("#CountrySelectAll_ID_ws").on('click', function()
$('.multiselect').find("input:checkbox").prop('checked',true);
})
});
$(function() {
$("#CountrySelectNone_ID_ws").on('click', function()
{
$('.multiselect').find("input:checkbox").prop('checked',false);
})
});
Upvotes: 1
Views: 1056
Reputation: 19319
Here is an example of ticking and un-ticking all the checkboxes in the div with the highlight formatting:
$('#CountrySelectAll_ID_ws').on('click', function(e) {
tickAll(true);
});
$('#CountrySelectNone_ID_ws').on('click', function(e) {
tickAll(false);
});
// click on any checkbox
$('.multiselect input').on('click', function() {
var input = $(this).parent();
if (input.hasClass('col-on')) {
input.removeClass('col-on');
} else {
input.addClass('col-on');
}
});
// handle buttons
function tickAll(status) {
$('.multiselect input').each(function(idx) {
$(this).prop('checked', status)
if(status) {
$(this).parent().addClass('col-on');
} else {
$(this).parent().removeClass('col-on');
}
})
};
.multiselect {
width: 20em;
height: 15em;
border: solid 1px #c0c0c0;
overflow: auto;
}
.multiselect label {
display: block;
}
.col-on {
color: #ffffff;
background-color: #000099;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<input type="button" id="CountrySelectAll_ID_ws" value='Tick all' />
<input type="button" id="CountrySelectNone_ID_ws" value='UNtick' />
<div class="multiselect">
<br>
<label>
<input type="checkbox" name="option[]" value="1" />Germany</label>
<label>
<input type="checkbox" name="option[]" value="2" />France</label>
<label>
<input type="checkbox" name="option[]" value="7" />Spain</label>
</div>
Upvotes: 1