I am trying to create a dropdown list with checkboxes, where the values in the dropdown are dependent on outer checkboxes.
This is what i currently have.
HTML
<div class="region-checkboxes">
<input type="checkbox" name="na" value="NA" />
<span style="width:25px;display:inline-block;">NA</span>
<input type='checkbox' name="eu" value="EU" />
<span style="width:25px;display:inline-block;">EU</span>
<input type='checkbox' name="apac" value="APAC" />
<span style="width:40px;display:inline-block;">APAC</span>
<input type='checkbox' name="cn" value="CN"/>
<span style="width:25px;display:inline-block;">CN</span>
JS
var allvalue = []
var checkboxes = document.querySelectorAll('.region-checkboxes input');
checkboxes.forEach(function(item) {
item.addEventListener('click', onChangeCheckbox);
});
function onChangeCheckbox (checkbox) {
console.log(checkbox.currentTarget.value);
if (checkbox.currentTarget.checked) {
allvalue.push(checkbox.currentTarget.value);
allvalue.sort();
}
else {
var index = allvalue.indexOf(checkbox.currentTarget.value);
if(index > -1){
allvalue.splice(index,1);
}
}
}
What I am trying to do is based on the checkboxes selected, it will populate a checkbox dropdown list with other values. In this example, it will populate sub-regions based on the regions you select.
So the dropdown will populate ["NA","LA"] if you select the "NA" check box, ["WEU", "EEU", "RU"] if you select the "EU" check box, if you select both, it will populate the drop list with both arrays. Here is what I have in mind.
http://jsfiddle.net/evfnLn0x/712/
So now the problem is. I do not know how to set up my code so it does dynamically. I also need the dropdown checkbox list to remove subregions when the outer checkbox gets unselected. I've been struggling all night with this.