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);
})
});