Other users have already provided interesting solutions, but here is an alternative if you find interesting, I will put in javascript and jquery:
Inversion of Selections
JQUERY
$(document).on('click', '#all', function (e) {
$('input[type="checkbox"]').not('#all').each(function() {
var checked = $(this).is(':checked');
checked ? $(this).prop('checked', false) : $(this).prop('checked', true);
});
});
JAVASCRIPT
document.getElementById("all").addEventListener("click", function(){
checkBoxs = document.querySelectorAll('input[type="checkbox"]:not([id=all])');
//"Hack": http://toddmotto.com/ditch-the-array-foreach-call-nodelist-hack/
[].forEach.call(checkBoxs, function(checkbox) {
checkbox.checked = checkbox.checked ? false : true;
});
});
Example: https://jsfiddle.net/1Lxwsae3/3/
EDIT
Select/Unselect All
var checkedAll = false;
//JQUERY
$(document).on('click', '#all', function (e) {
$('input[type="checkbox"]').not('#all').each(function() {
//Verificamos se é a hora de dar checked a todos ou tirar;
checkedAll ? $(this).prop('checked', false) : $(this).prop('checked', true);
});
//Invertemos ao final da execução, caso a última tenha sido true para checar todos, tornamos ele false para o próximo clique;
checkedAll = checkedAll ? false : true;
});
//JAVASCRIPT
document.getElementById("all").addEventListener("click", function(){
checkBoxs = document.querySelectorAll('input[type="checkbox"]:not([id=all])');
//"Hack": http://toddmotto.com/ditch-the-array-foreach-call-nodelist-hack/
[].forEach.call(checkBoxs, function(checkbox) {
//Verificamos se é a hora de dar checked a todos ou tirar;
checkbox.checked = checkedAll ? false : true;
});
//Invertemos ao final da execução, caso a última tenha sido true para checar todos, tornamos ele false para o próximo clique;
checkedAll = checkedAll ? false : true;
});
Example: https://jsfiddle.net/ze9qo4yu/1/
If you have any questions or are not what you are looking for, please let us know.
That example could also be better built...
– CesarMiguel
What is your question? Is it how to select all checkboxes for marking? Is it how to make them check? Something else?
– mgibsonbr
is to mark and uncheck all checkboxes with only the last checkbox. I edited jsfiddle.
– akm
@akm, have you noticed that all your html is poorly built? Quotation marks are missing" on each element..
– CesarMiguel