just want to ask if how can I set a default values according to the number of checkboxes available. For example I have 3 checkboxes and I want to have a default values of 0,0,0 since I have 3 checkboxes. And if the user click the first checkbox the first 0 will be replaced with 1 since the value of the first checkbox has a value of 1 and the same as the rest checkboxes. And if the user uncheck the first checkbox it will return to it's default value which is 0. I have embedded my snippet. Thank you guys in advance.
$('input#category').on('change', function () {
var selectedCategories = $('.category:checkbox:checked').map(function () {
return this.value;
}).get();
$('#post_category').val(selectedCategories);
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>
<input type="checkbox" value="1" id="category" name="category" class="category category_1">Category 1
<input type="checkbox" value="2" id="category" name="category" class="category category_2">Category 2
<input type="checkbox" value="3" id="category" name="category" class="category category_3">Category 3
<br />
<h5>Result:</h5>
<input type="text" id="post_category">
idattribute.. Check thatid="category"is multiple times