Home > other >  Convert .val() to float to return numbers from select options as float?
Convert .val() to float to return numbers from select options as float?

Time:01-20

Why does .val() return numbers as text instead of float, using apostrophes inside the array?

<select id = "multiselect" multiple = "multiple"> 
  <option value="1" >Text 1</option>
  <option value="2" >Text 2</option>
  <option value="3">Text 3</option>
  <option value="4" >Text 4</option>
</select>

var selectedvalues = $('#multiselect').val();

console.log(selectedvalues); // returns ['1','2','4'] instead of [1,2,4]

Can I simply convert the array to float/numbers?

or is there another simple way to extract all values of selected options and put inside an array?

I tried parseFloat but it doesn't seem to work with array as it only returns 1.

CodePudding user response:

You have to call parseFloat() on each array element.

var selectedValues = $('#multiselect').val().map(parseFloat);
  • Related