I’m trying to make a form which checks if a certain option is selected from a “select” tag. Here is my current HTML:
JavaScript
x
9
1
<select onchange="yesnoCheck()">
2
<option id="noCheck" value="">Valitse automerkkisi</option>
3
<option id="noCheck" value="lada">Lada</option>
4
<option id="noCheck" value="mosse">Mosse</option>
5
<option id="noCheck" value="volga">Volga</option>
6
<option id="noCheck" value="vartburg">Vartburg</option>
7
<option id="yesCheck" value="other">Muu</option>
8
</select>
9
This is the div element which should become visible after “Muu” is selected:
JavaScript
1
4
1
<div id="ifYes" style="display: none;">
2
<label for="car">Muu, mikä?</label> <input type="text" id="car" name="car" /><br />
3
</div>
4
And here is the JavaScript I’m trying to use:
JavaScript
1
10
10
1
<script type="text/javascript">
2
function yesnoCheck() {
3
if (document.getElementById("yesCheck").checked) {
4
document.getElementById("ifYes").style.display = "block";
5
} else {
6
document.getElementById("ifYes").style.display = "none";
7
}
8
}
9
</script>
10
But it’s not working…
Advertisement
Answer
here you go:
JavaScript
1
8
1
function yesnoCheck(that) {
2
if (that.value == "other") {
3
alert("check");
4
document.getElementById("ifYes").style.display = "block";
5
} else {
6
document.getElementById("ifYes").style.display = "none";
7
}
8
}
JavaScript
1
12
12
1
<select onchange="yesnoCheck(this);">
2
<option value="">Valitse automerkkisi</option>
3
<option value="lada">Lada</option>
4
<option value="mosse">Mosse</option>
5
<option value="volga">Volga</option>
6
<option value="vartburg">Vartburg</option>
7
<option value="other">Muu</option>
8
</select>
9
10
<div id="ifYes" style="display: none;">
11
<label for="car">Muu, mikä?</label> <input type="text" id="car" name="car" /><br />
12
</div>