The selection of the “building” drop-down should equal zero. It is on the the first option. Function search() is executed when the button beside the drop-down is selected. For some reason the alert is returning “It didn’t work.”
JavaScript
x
14
14
1
<script>
2
var a = document.getElementById("building").selectedIndex;
3
4
function search() {
5
if (a === 0) {
6
window.alert("It worked.");
7
event.preventDefault();
8
} else {
9
window.alert("It didn't work.");
10
event.preventDefault();
11
}
12
}
13
</script>
14
Here is the selection drop-down.
JavaScript
1
17
17
1
<form id="apartmentSelection">
2
<br>
3
Building:
4
<select id="building">
5
<option value="all">All</option>
6
<option value="1">1</option>
7
<option value="2">2</option>
8
<option value="3">3</option>
9
<option value="4">4</option>
10
<option value="5">5</option>
11
<option value="6">6</option>
12
<option value="7">7</option>
13
<option value="8">8</option>
14
<option value="9">9</option>
15
</select>
16
</form>
17
And here is the button.
JavaScript
1
2
1
<button onclick="search()">Search</button>
2
Advertisement
Answer
Var a
is in the wrong place and you don’t need event.preventDefault();
twice.
JavaScript
1
10
10
1
function search() {
2
event.preventDefault();
3
var a = document.getElementById("building").selectedIndex;
4
if(a === 0){
5
alert("It worked.");
6
} else {
7
alert("It didn't work.");
8
}
9
}
10