Skip to content

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

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

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

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.



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

var selectedValues = $('#multiselect').val().map(parseFloat);
User contributions licensed under: CC BY-SA
8 People found this is helpful