0

I want to check the function call in "IF condition", but it's not working. I'm getting the output as "undefined "My program output supposed to be: If user Select a car, then response will be accordingly. Is my method to get value is wrong?

function carPickUp() {
  var pickedCar = document.getElementById("cars").value;
  return pickedCar;
}

function showCar() {
  var message;
  if (carPickUp() == "ROLLS") {
    message = "Great choice";
  } else if (carPickUp() == "AUDI") {
    message = "best choice";
  } else if (carPickUp() == "VOLVO") {
    message = "Good choice";
  } else if (carPickUp() == "FORD") {
    message = "Good choice";
  }
  document.getElementById("demo1").innerHTML = message;
}
<h2>The select Element</h2>
<p>The select element defines a drop-down list:</p>
<form action="/action_page.php">
  <label for="cars">Choose a car:</label>
  <select id="cars" name="cars">
    <option value="volvo">VOLVO</option>
    <option value="ford">FORD</option>
    <option value="rolls">ROLLS</option>
    <option value="audi">AUDI</option>
  </select>
  <input type="button" onclick="showCar()" value="Submit" />
</form>
<p id="demo1"></p>
mplungjan
  • 169,008
  • 28
  • 173
  • 236

0 Answers0