Within my UI
, I have a select field that, on change, populates corresponding data in a div with nested ul
and li
elements. What I am attempting to achieve is to convert the selected items within the list (which include checkboxes) into an object of arrays upon button click.
My Progress So Far
$(document).ready(function() {
$("#btn-search").hide();
var Data = {
"India A": [
"Mumbai",
"Delhi",
"Kolkata"
],
"India B": [
"Bangalore",
"Chennai"
]
}
var CountersName = Object.keys(Data)
let dropdown = $("#counterNames")
dropdown.append('<option selected="true" disabled>Select Counter</option>');
for (var i = 0; i < CountersName.length; i++) {
$('<option/>').val(CountersName[i]).html(CountersName[i]).appendTo('#counterNames');
}
$("#counterNames").on('change', function() {
$(".card").show();
$("#btn-search").show();
var value = $(this).val();
$(".card-header").text(value);
var ul = document.getElementById(".list-group");
ul_innerhtml = "";
for (i = 0; i < Data[value].length; i++) {
title = Data[value][i];
var ul_innerhtml = ul_innerhtml + '<li class="list-group-item">' + title + '<label class="switch "><input type="checkbox" class="success"><span class="slider round"> </span></label></li>';
}
$(".list-group").html(ul_innerhtml);
})
});
$("button").click(function() {
var selected = new Array();
$("input:checkbox[name=type]:checked").each(function() {
selected.push($(this).val());
});
console.log(selected)
});
.switch {
position: relative;
display: inline-block;
width: 60px;
height: 34px;
float: right;
}
/* Hide default HTML checkbox */
.switch input {
display: none;
}
.slider {
position: absolute;
cursor: pointer;
top: 0;
left: 0;
right: 0;
bottom: 0;
background-color: #ccc;
-webkit-transition: .4s;
transition: .4s;
}
.slider:before {
position: absolute;
content: "";
height: 26px;
width: 26px;
left: 4px;
bottom: 4px;
background-color: white;
-webkit-transition: .4s;
transition: .4s;
}
input.success:checked+.slider {
background-color: #8bc34a;
}
input:checked+.slider:before {
-webkit-transform: translateX(26px);
-ms-transform: translateX(26px);
transform: translateX(26px);
}
.slider.round {
border-radius: 34px;
}
.slider.round:before {
border-radius: 50%;
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/4.3.1/css/bootstrap.min.css">
<div class="container">
<div class="col-xs-12 col-sm-12 col-md-6 col-lg-6">
<label for="counterNames">Select Counter:</label>
<select class="form-control" id="counterNames">
</select>
<div class="card" style="margin: 10px 0; display: none;">
<div class="card-header"></div>
<ul class="list-group list-group-flush">
</ul>
</div>
</div>
<button id="btn-search" class="btn btn-default commonButton" type="submit">
<i class="fa fa-search"></i> Go
</button>
</div>
Upon clicking the 'Go' button, I aim to retrieve the selected option from the dropdown along with the checked items like this
{India A:["Mumbai","Delhi","Kolkata"]}
when India A
is chosen. However, my attempt at converting this into an array has resulted in empty output.