Hello guys i have a question here. So in this problem i can’t display the text from selected option while i click order button. For the result i need the selected option i choose and display it into a div. Any suggestion what i must change or add here?
P.S : Sorry i’m still learning here, i hope my question didn’t confuse anyone here..
html
JavaScript
x
23
23
1
<div class="container">
2
<div class="container-fluid text-center">
3
<h2 style="font-size:70px; font-family:Lucida Console;">MENU</h2>
4
<br />
5
<div class="row">
6
<div class="col-md-6">
7
<select class="form-select form-select-lg mb-3" id="category_select" onChange='handleChange()'>
8
<option value="Food">Food</option>
9
<option value="Drink">Drink</option>
10
</select>
11
</div>
12
<br />
13
<div class="col-md-6">
14
<select class="form-select form-select-lg mb-3" id="type_select"></select>
15
</div>
16
</div>
17
</div>
18
</div>
19
<br />
20
<button type="button" style="width:50%; margin-left:25%; margin-right:25%">Order</button>
21
<br />
22
<div class="result"></div>
23
js
JavaScript
1
75
75
1
var data = {
2
Food: [
3
{
4
id: 1,
5
name: 'Fried Rice',
6
price: '10.000'
7
},
8
{
9
id: 2,
10
name: 'Fried Noodle',
11
price: '9.000'
12
},
13
{
14
id: 3,
15
name: 'Pancake',
16
price: '8.500'
17
},
18
{
19
id: 4,
20
name: 'French Fries',
21
price: '7.500'
22
}
23
],
24
Drink: [
25
{
26
id: 1,
27
name: 'Cola',
28
price: '4.600'
29
},
30
{
31
id: 2,
32
name: 'Orange Juice',
33
price: '5.400'
34
},
35
{
36
id: 3,
37
name: 'Mineral Water',
38
price: '3.500'
39
},
40
{
41
id: 4,
42
name: 'Coffee',
43
price: '5.800'
44
}
45
]
46
}
47
48
function handleChange() {
49
var x = document.getElementById("category_select").value;
50
51
var dataOptions = data[x]
52
var dataSelect = document.getElementById('type_select')
53
dataSelect.innerHTML = ''
54
55
dataOptions.forEach(function (option) {
56
var optionEle = document.createElement('option')
57
optionEle.value = option.id
58
optionEle.label = option.name
59
60
dataSelect.appendChild(optionEle)
61
})
62
63
}
64
handleChange()
65
66
$(document).ready(function () {
67
$("button").click(function () {
68
var selectMenu = [];
69
$.each($("#type_select"), function () {
70
selectMenu.push($(this)).val();
71
});
72
$(".result").html(selectMenu);
73
});
74
});
75
Advertisement
Answer
You should declare the array outside the click handler function. Also , you probably want to store the label attribute of the selected option. Join the array items with ,
before showing in the element:
JavaScript
1
72
72
1
var data = {
2
Food: [
3
{
4
id: 1,
5
name: 'Fried Rice',
6
price: '10.000'
7
},
8
{
9
id: 2,
10
name: 'Fried Noodle',
11
price: '9.000'
12
},
13
{
14
id: 3,
15
name: 'Pancake',
16
price: '8.500'
17
},
18
{
19
id: 4,
20
name: 'French Fries',
21
price: '7.500'
22
}
23
],
24
Drink: [
25
{
26
id: 1,
27
name: 'Cola',
28
price: '4.600'
29
},
30
{
31
id: 2,
32
name: 'Orange Juice',
33
price: '5.400'
34
},
35
{
36
id: 3,
37
name: 'Mineral Water',
38
price: '3.500'
39
},
40
{
41
id: 4,
42
name: 'Coffee',
43
price: '5.800'
44
}
45
]
46
}
47
48
function handleChange() {
49
var x = document.getElementById("category_select").value;
50
51
var dataOptions = data[x]
52
var dataSelect = document.getElementById('type_select')
53
dataSelect.innerHTML = ''
54
55
dataOptions.forEach(function (option) {
56
var optionEle = document.createElement('option')
57
optionEle.value = option.id
58
optionEle.label = option.name
59
60
dataSelect.appendChild(optionEle)
61
})
62
63
}
64
handleChange()
65
66
$(document).ready(function () {
67
var selectMenu = [];
68
$("button").click(function () {
69
selectMenu.push($("#type_select option:selected").attr('label'));
70
$(".result").html(selectMenu.join(', '));
71
});
72
});
JavaScript
1
23
23
1
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
2
<div class="container">
3
<div class="container-fluid text-center">
4
<h2 style="font-size:70px; font-family:Lucida Console;">MENU</h2>
5
<br />
6
<div class="row">
7
<div class="col-md-6">
8
<select class="form-select form-select-lg mb-3" id="category_select" onChange='handleChange()'>
9
<option value="Food">Food</option>
10
<option value="Drink">Drink</option>
11
</select>
12
</div>
13
<br />
14
<div class="col-md-6">
15
<select class="form-select form-select-lg mb-3" id="type_select"></select>
16
</div>
17
</div>
18
</div>
19
</div>
20
<br />
21
<button type="button" style="width:50%; margin-left:25%; margin-right:25%">Order</button>
22
<br />
23
<div class="result"></div>