javascripthtml-selectparseint

convert string to integer of selected option


Hi I would like to convert the values of the selected options into integers. Unfortunately its not working.

Example HTML:

<label for="ausgangssprache">Ausgangssprache</label>
<select name="ausgangssprache" size="1" id="ausgangssprache"> <option value="0.78">Deutsch</option> <option value="0.78">Englisch</option> <option value="0.78" selected>Franz&ouml;sisch</option> <option value="0.78">Spanisch</option> <option value="0.78">Italienisch</option> <option value="1.1">Arabisch</option></select>

Javascript:

var a = document.getElementById("ausgangssprache");
var ausgangssprache = a.options[a.selectedIndex].value;

I've tried with parseInt() this both, without getting the selected value, when testing via alert()

var a = parseInt(document.getElementById("ausgangssprache"));
var ausgangssprache = a.options[a.selectedIndex].value;

var a = document.getElementById("ausgangssprache");
var ausgangssprache = parseInt(a.options[a.selectedIndex].value);

Solution

  • var a = parseInt(document.getElementById("ausgangssprache").value);
    

    you can use this, unless you're doing multiple select. to not use parseInt, assign value as a int like value=3 instead of value="3"