HTML
xxxxxxxxxx
1
<!-- With placeholder and default selected option-->
2
<div class="row">
3
<div class="col-4 select-outline">
4
<select class="mdb-select md-form md-outline colorful-select dropdown-primary"
5
id="unit_type" name="unit_type" data-placeholder="Please select"
6
onchange="setVal();">
7
<option value="1-2">1-2</option>
8
<option value="3-4">3-4</option>
9
<option value="5-6" selected>5-6</option>
10
</select>
11
<!-- default should selected "5-6" (as selected),
12
placeholder should be "Please select" (should not show),
13
js select val should be "5-6" on load -->
14
<label>type</label>
15
</div>
16
<div class="col-2">
17
<span id="unit_val"></span>
18
<!-- default should show "5-6" on load -->
19
</div>
20
</div>
21
<!-- Without default selected option-->
22
<div class="row">
23
<div class="col-4 select-outline">
24
<select class="mdb-select md-form md-outline colorful-select dropdown-primary"
25
id="unit_type_2" name="unit_type_2" data-placeholder="Please select"
26
onchange="setVal();">
27
<option value="1-2">1-2</option>
28
<option value="3-4">3-4</option>
29
<option value="5-6">5-6</option>
30
</select>
31
<!-- default should selected "" (as no option selected),
32
placeholder should be "Please select",
33
js select val should be "" on load -->
34
<label>type</label>
35
</div>
36
<div class="col-2">
37
<span id="unit_val_2"></span>
38
<!-- default should show "" on load -->
39
</div>
40
</div>
41
<!-- Control (Without placeholder)-->
42
<div class="row">
43
<div class="col-4 select-outline">
44
<select class="mdb-select md-form md-outline colorful-select dropdown-primary"
45
id="unit_type_3" name="unit_type_3"
46
onchange="setVal();">
47
<option value="1-2">1-2</option>
48
<option value="3-4">3-4</option>
49
<option value="5-6" selected>5-6</option>
50
</select>
51
<!-- default should selected "5-6" (as selected),
52
no placeholder,
53
js select val should be "5-6" on load -->
54
<label>type</label>
55
</div>
56
<div class="col-2">
57
<span id="unit_val_3"></span>
58
<!-- default should show "5-6" on load -->
59
</div>
60
</div>
61
CSS
1
1
JS
xxxxxxxxxx
1
// Material Select Initialization
2
$(document).ready(function() {
3
$('.mdb-select').materialSelect();
4
setVal();
5
});
6
7
function setVal() {
8
var unit_type = $('#unit_type').val();
9
var unit_type_2 = $('#unit_type_2').val();
10
var unit_type_3 = $('#unit_type_3').val();
11
$('#unit_val').text(unit_type);
12
$('#unit_val_2').text(unit_type_2);
13
$('#unit_val_3').text(unit_type_3);
14
}
Console errors: 0