Prevent user from deselecting last checked checkbox.
jQuery (original answer).
$('input[type="checkbox"][name="chkBx"]').on('change',function(){
var getArrVal = $('input[type="checkbox"][name="chkBx"]:checked').map(function(){
return this.value;
}).toArray();
if(getArrVal.length){
//execute the code
$('#msg').html(getArrVal.toString());
} else {
$(this).prop("checked",true);
$('#msg').html("At least one value must be checked!");
return false;
}
});
UPDATED ANSWER 2019-05-31
Plain JS
let i,_x000D_
el = document.querySelectorAll('input[type="checkbox"][name="chkBx"]'),_x000D_
msg = document.getElementById('msg'),_x000D_
onChange = function(ev){_x000D_
ev.preventDefault();_x000D_
let _this = this,_x000D_
arrVal = Array.prototype.slice.call(_x000D_
document.querySelectorAll('input[type="checkbox"][name="chkBx"]:checked'))_x000D_
.map(function(cur){return cur.value});_x000D_
_x000D_
if(arrVal.length){_x000D_
msg.innerHTML = JSON.stringify(arrVal);_x000D_
} else {_x000D_
_this.checked=true;_x000D_
msg.innerHTML = "At least one value must be checked!";_x000D_
}_x000D_
};_x000D_
_x000D_
for(i=el.length;i--;){el[i].addEventListener('change',onChange,false);}
_x000D_
<label><input type="checkbox" name="chkBx" value="value1" checked> Value1</label>_x000D_
<label><input type="checkbox" name="chkBx" value="value2"> Value2</label>_x000D_
<label><input type="checkbox" name="chkBx" value="value3"> Value3</label>_x000D_
<div id="msg"></div>
_x000D_