I'm trying to use js to display a users selection in an HTML dropdown list after their selection is submitted. I've searched for hours and tried several different methods and none of them have worked so far. This is my current code:
<script>
var e = document.getElementById("user_engine");
var strUser = e.options[e.selectedIndex].text;
function showEngineChoice() {
document.getElementById('engine').innerHTML =
document.getElementById("strUser").value;
}
</script>
<label><b>What type of engine would you like in your favorite car?</b></label>
<select name="engine_types" id="user_engine">
<option value="1">In-Line V6</option>
<option value="2">3.8L 4-cylinder</option>
<option value="3">Twin Turbo V10</option>
<option value="4">RS-68 Rocket Engine</option>
</select>
<input type="submit" onclick="showEngineChoice()"><br />
<label>You chose: </label><br />
<p><span id="engine"></span></p><br />
<label>Nice Choice!</label>
I've tried a few other methods, and each time I run the code, clicking the submit button after making a selection does nothing. What am I not doing right?
#strUserelement defined ?var e = document.getElementById("user_engine");can be defined , since your<script></script>is preceding the#user_engineelement AND your code appears to be asynchronous.