I have a list menu like this:
$("ul.list-dist li a").click(function(e) {
e.preventDefault();
$("#distId").val($(this).attr("class"));
var options = $("#distId option");
var activeClass = $(this).attr("class");
options.each(function(i, e) {
var t = $(this);
if (t.val() == activeClass) {
t.show();
} else {
t.hide();
}
})
});
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<li><a href="#">city-one</a>
<ul class="list-dist">
<li><a id="" class="dist 1" value="dist 1">dist 1</a></li>
<li><a id="" class="dist 2" value="dist 2">dist 2</a></li>
<li><a id="" class="dist 3" value="dist 3">dist 3</a></li>
</ul>
</li>
I can get value from menu list and add selected value to option this:
<select name="distId" id="distId">
<option value="dist 1">dist 1</option>
<option value="dist 2">dist 2</option>
<option value="dist 3">dist 3</option>
</select>
Now I want after the value from the menu list add the selected value to the option then the data will be taken from the json into this sub option:
<select id="wardId">
</select>
Idea is: when i select dist 1
from list menu, option will show dist 1
and sub option will show wards data (ward 1, ward 2...) contained in dist 1
(from json)
This is my json:
{
"city-one": {
"dist 1": [
{"name": "wards 1"},
{"name": "wards 2"},
{"name": "wards 3"}
],
"dist 2": [
{"name": "wards 4"},
{"name": "wards 5"},
{"name": "wards 6"}
]
},
"city-two": {
"dist zxc": [
{"name": "wards z"},
{"name": "wards x"},
{"name": "wards c"}
],
"city xyz": [
{"name": "wards x"},
{"name": "wards y"},
{"name": "wards z"}
],
"dist abc": [
{"name": "wards a"},
{"name": "wards b"},
{"name": "wards c"}
]
}
}
Sorry if my explanation is confusing. Thanks for your help.