so I have a module popup window with a div. In it are several checkboxes, what I want to do is after the user has finished checking or unchecking the boxes he wants and clicks the done button. I want jQuery to dump all the selected values into an arrray.
Originally I just added whatever was checked into an Array, but then I had problems with trying to remove the checked item from the Array if the user decided to uncheck it. Doing it the other way above seems to be the similar method.
I created a Codepen here.
My jQuery
$('.select-roles').click(function(){
var selectedRole = $(this).html();
var selectedArray = new Array();
$('roleId input:checked').each(function() {
selectedArray.push($(this).val());
console.log($(this).val());
});
console.log('modalName = '+modalName);
console.log('selected array contains: '+selectedArray);
});
// displays the value of the checked checkbox in the console
$('input[type=checkbox]').click(function() {
if($(this).is(':checked')){
console.log( $(this).val())
}
});
HTML
<div id="modal-TV">
<div class="modal-top">
<div class="modal-title">Actor: Select one or more genres below</div>
</div>
<div class="modal-choices">
<div class="modal-col-1">
<ul>
<li><input type="checkbox" value="Adult Animated">Adult Animated</li>
<li><input type="checkbox" value="Anime">Anime</li>
<li><input type="checkbox" value="Award Shows">Award Shows</li>
<li><input type="checkbox" value="Behind The Scenes">Behind The Scenes</li>
<li><input type="checkbox" value="Christian">Christian</li>
</ul>
</div>
<!-- modal-choices -->
<div class="modal-dashes"></div>
<div class="modal-buttons">
<ul>
<a class="close" title="Cancel"><li>Cancel</li></a>
<li class="select-roles">Done</li>
</ul>
</div>
</div>
<!-- modal-TV -->