To store or get selected checkbox value for further use such as fetching or sending data from server may be there lots of way to do this,but after trying lots of method i found below code is perfect for this task.It’s easy,simple and few line code.I’m approaching this solution by using of jQuery, so let’s don’t waste a time and start copying below code make your task easy.
function Populate(){
vals = $('input[type="checkbox"]:checked').map(function() {
return this.value;
}).get().join(',');
console.log(vals);
$('#adminRole').val(vals);
}
$('input[type="checkbox"]').on('change', function() {
Populate()
}).change();
Explanation:
In the above code i create “Populate” method which will be always trigger whenever checkbox checked or unchecked and i”m store or remove the checkbox value in “Populate” method and here i’m also checking that value should not be duplicate.
If you have any doubt please comment below.