Why does .val()
return numbers as text instead of float, using apostrophes inside the array?
JavaScript
x
11
11
1
<select id = "multiselect" multiple = "multiple">
2
<option value="1" class="selected">Text 1</option>
3
<option value="2" class="selected">Text 2</option>
4
<option value="3">Text 3</option>
5
<option value="4" class="selected">Text 4</option>
6
</select>
7
8
var selectedvalues = $('#multiselect').val();
9
10
console.log(selectedvalues); // returns ['1','2','4'] instead of [1,2,4]
11
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.
Advertisement
Answer
You have to call parseFloat()
on each array element.
JavaScript
1
2
1
var selectedValues = $('#multiselect').val().map(parseFloat);
2