Pregunta

I'm trying to get the value of a drop down box and keep getting the error Uncaught TypeError: Cannot read property 'selectedIndex' of null.

Heres my code:

JS

expirationYearSelectedIndex = document.getElementById("selYear").selectedIndex;
    console.log("expirationYearSelectedIndex = " + expirationYearSelectedIndex);

    expirationYear = document.getElementById("selYear").options[expirationYearSelectedIndex].value;
    console.log("expirationYear = " + expirationYear);

HTML

<form action="">
    <select id="SelYear" name="selYear">
        <option selected value=""> Select a Major....</option>
        <option value="Comptuer Information Systems"> CIS</option>
        <option value="Math"> M</option>
        <option value="CS"> CS</option>
        <option value="History"> H</option>
    </select>
</form>
¿Fue útil?

Solución

Missed the capital S on the ID name <select id="SelYear" name="selYear">

document.getElementById("SelYear");
Licenciado bajo: CC-BY-SA con atribución
No afiliado a StackOverflow
scroll top