I am trying to use my select in HTML, bring in the values, then depending on the value, assign a different value to a variable. It's for an order form that will eventually give an updated price list based on what options the customer selects.
The logic goes as follows:
- select an option
- the option's value is assigned to a variable
- based on the value, a numeric value is then assigned to that variable using a conditional statement
At this point I can get it to return the initial value, but once I invoke the conditional statement, it won't return anything.
I'm not receiving any errors, so I am just a little bit confused.
function fn1() {
let cpu = document.getElementById("cpuOption").value;
return cpu;
cpuPrice = 0;
if (cpu == cpu_1) {
cpuPrice = 200;
} else if (cpu == cpu_2) {
cpuPrice = 300;
} else {
cpuPrice = 400;
}
document.getElementById("test").innerHTML = cpuPrice;
}
<form>
<label for="cpuOption">Desired CPU Configuration</label>
<br>
<select id="cpuOption" onchange="fn1()">
<option value="none" selected>select</option>
<option value="cpu_1">cpu-1</option>
<option value="cpu_2">cpu-2</option>
<option value="cpu_3">cpu-3</option>
</select>
</form>
<h2>Check it out!</h2>
<button onclick="fn1()">Click me</button>
<div id="test"></div>