I am working on an HTML form that includes a dropdown list with four different names to choose from.
window.onload = function(){
document.getElementById("submit").onclick = displaySelectedStudent;
}
function displaySelectedStudent(){
if(document.getElementById("mary jones").value == "mary jones"){
document.getElementById("students").innerHTML = "Mary Jones";
}
else if(document.getElementById("jane doe").value == "jane doe"){
document.getElementById("students").innerHTML = "Jane Doe";
}
else if(document.getElementById("henry miller").value == "henry miller"){
document.getElementById("students").innerHTML = "Henry Miller";
}
else if(document.getElementById("john smith").value == "john smith"){
document.getElementById("students").innerHTML = "John Smith";
}
}
<div id="students">
<form id="getStudent" action="" method="GET">
<select name="students">
<option value="john smith" id="john smith">John Smith</option>
<option value="jane doe" id="jane doe">Jane Doe</option>
<option value="henry miller" id="henry miller">Henry Miller</option>
<option value="mary jones" id="mary jones">Mary Jones</option>
</select>
<br><br>
<input id="submit" type="submit">
</form>
Upon clicking the submit button, a JavaScript function is called to display the name of the selected student. However, only the result of the first if statement is currently being displayed. I suspect that I need to pass the form data value into the function, but I am unsure how to accomplish this. Here is the current JavaScript code I have developed for this purpose.