I have a dropdown list which looks like this:
JavaScript
x
6
1
<select id="cityID">
2
<option value="mission">Mission</option>
3
<option value="bakersfield">Bakersfield</option>
4
<option value="knoxville">Knoxville</option>
5
</select>
6
And my code to get the value is:
JavaScript
1
8
1
var select = document.getElementById('cityID');
2
var text = select.options[select.selectedIndex].text;
3
text.innerHTML = cityID.value;
4
5
text.onchange = function(e) {
6
text.innerHTML = e.target.value;
7
}
8
The value always chooses the first item. How can I get it to accept the cityID and change the page,
I’m sure its a formatting or typo or wrong value ?
Advertisement
Answer
You could achieve this using addEventListener also.
JavaScript
1
6
1
var select = document.getElementById('cityID');
2
var textEl = document.getElementById("text")
3
select.addEventListener("change", (e) => {
4
textEl.innerText = e.target.value;
5
})
6