0

I have the following condensed form

<form name="thecars">
<select name="cars">
    <option value="mustang">Mustang</option>
    <option value="pinto">Pinto</option>
    <option value="pinto">Chevelle</option>
    <option value="pinto">Other</option>
</select>
</form>

I am trying to get the value of the selected car by the following but it is not working

selectedCar = document.forms["thecars"].elements["cars"].options[thecars.cars.options.selectedIndex].value;

3 Answers 3

3

Correct code would be:

var oForm = document.forms["thecars"];
var oDDL = oForm.elements["cars"];
var selectedCar = oDDL.value;

You can't get reference to the form by just using its name.

Sign up to request clarification or add additional context in comments.

Comments

1

Assign an ID to your select element:

<select name="cars" id="cars">

And you can get the value like this:

document.getElementById('cars').value

Comments

0

You are missing an =. Change this

<form name "thecars">

To this

<form name="thecars">

1 Comment

Oh sorry that was a typo.The = is there.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.