2

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);
aynber
  • 22,380
  • 8
  • 50
  • 63
Krystian
  • 887
  • 5
  • 21
  • 52
  • 2
    Try `Number(a.options[a.selectedIndex].value);` – Rayon Mar 20 '17 at 05:27
  • 1
    Possible duplicate of [How do I convert a string into an integer in JavaScript?](http://stackoverflow.com/questions/1133770/how-do-i-convert-a-string-into-an-integer-in-javascript) – Max Mar 20 '17 at 05:29

1 Answers1

4
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"

Chris Cui
  • 117
  • 5