xxxxxxxxxx
1
2
<div class="container align-content-center">
3
<br>
4
<br>
5
<p>This example shows how when a select field is hidden with CSS and then displayed it renders with part of the perimeter of the field covering the label, however using a function on window load to hide the select field allows it to render correctly. To see this happen set field one to "three" to display field two (which will display incorrectly) and set field three to "three" to display field four (which will display correctly).</p>
6
<br>
7
8
<form action="" method="post" class="form g-3 needs-validation" enctype="multipart/form-data" role="form"
9
novalidate>
10
<input id="csrf_token" name="csrf_token" type="hidden" value="ImUxZGI4ZjViMjdlZWYxNGIyNTU4Zjk3Y2NmYmRjMTg4M2E4YzMyNjEi.ZNFBOw.OHsWXjVQa30JZDfOmDUqBzW87Qc">
11
<div id=field_one_div class="mb-4">
12
<select class="select" id="field_one" name="field_one"><option value="one">one</option><option value="two">two</option><option value="three">three</option><option value="four">four</option></select>
13
<label class="form-label select-label" for="field_one">Field One</label>
14
15
</div>
16
<div id=field_two_div class="mb-4">
17
<select class="select" id="field_two" name="field_two"><option value="one">one</option><option value="two">two</option><option value="three">three</option><option value="four">four</option></select>
18
<label class="form-label select-label" for="field_two">Field Two</label>
19
</div>
20
<div id=field_three_div class="mb-4">
21
<select class="select" id="field_three" name="field_three"><option value="one">one</option><option value="two">two</option><option value="three">three</option><option value="four">four</option></select>
22
<label class="form-label select-label" for="field_three">Field Three</label>
23
24
</div>
25
<div id=field_four_div class="mb-4">
26
<select class="select" id="field_four" name="field_four"><option value="one">one</option><option value="two">two</option><option value="three">three</option><option value="four">four</option></select>
27
<label class="form-label select-label" for="field_four">Field Four</label>
28
</div>
29
<input id="csrf_token" name="csrf_token" type="hidden" value="ImUxZGI4ZjViMjdlZWYxNGIyNTU4Zjk3Y2NmYmRjMTg4M2E4YzMyNjEi.ZNFBOw.OHsWXjVQa30JZDfOmDUqBzW87Qc">
30
</form>
31
</div>
32
xxxxxxxxxx
1
#field_two_div {display: none;}
2
xxxxxxxxxx
1
window.onload = hide_fourth();
2
3
function hide_fourth() {
4
document.getElementById("field_four_div").style.display = "none";
5
}
6
7
function show_second() {
8
if (this.value == "three") {
9
document.getElementById("field_two_div").style.display = "block";
10
}
11
}
12
13
function show_fourth() {
14
if (this.value == "three") {
15
document.getElementById("field_four_div").style.display = "block";
16
}
17
}
18
19
document.getElementById("field_one").addEventListener("change", show_second);
20
document.getElementById("field_three").addEventListener("change", show_fourth);
21
Console errors: 0