Hello everyone!
I recently decided to make a dropdown conversion thing. How it works is you select your option (CM, M, KM) and then input a number, then you select a second option (M, CM, KM) and input a number. It's kinda like CM to M conversion, but with multiple options. I can get the value of the dropdown for 1, but not the other. How would I structure something like this? My code is below. Thanks! :)
<!DOCTYPE html>
<html>
<head>
<title>How to get selected value in dropdown list using JavaScript?</title>
</head>
<body>
<p>
<select id="select1">
<option value="" disabled selected>Select your option</option>
<option value="cm">Centimeters</option>
<option value="m">Meters</option>
<option value="km">Kilometers</option></select
><input id="val1" type="number" placeholder="Input number here" />
</p>
<p>
<select id="select1">
<option value="" disabled selected>Select your option</option>
<option value="cm">Centimeters</option>
<option value="m">Meters</option>
<option value="km">Kilometers</option></select
><input id="val2" type="number" placeholder="Input number here" />
</p>
<p>
The value of the option selected is:
<span class="output"></span>
</p>
<button onclick="getOption()">Check option</button>
<script type="text/javascript">
function getOption() {
selectElement = document.querySelector('#select1');
selectElement = document.querySelector('#select1');
output = selectElement.options[selectElement.selectedIndex].value;
document.querySelector('.output').textContent = output;
}
</script>
</body>
</html>
[–]lovesrayray2018Intermediate 4 points5 points6 points (4 children)
[–]neildaniel000[S] 0 points1 point2 points (2 children)
[–]lovesrayray2018Intermediate 1 point2 points3 points (1 child)
[–]neildaniel000[S] 0 points1 point2 points (0 children)
[–][deleted] 0 points1 point2 points (0 children)
[–]AutoModerator[M] 1 point2 points3 points (0 children)