I am trying to push values into an array from a multiple select dropdown box. I am able to do that but what happens is that they are not pushed into the array in the order i set it up to via splice. It is added in the order it is set up on the select box. For example if i select the options in the order of [467, 341, 344, 657, 677], my array shows [344, 467, 677, 341, 657]. I want the array to show in order I have selected the options. This is my code:
<select multiple class="yo">
<option value = "344">opt1</option>
<option value = "467">opt2</option>
<option value = "677">opt3</option>
<option value = "341">opt1</option>
<option value = "657">opt1</option>
</select>
var optVal = new Array();
var tArray;
$('.yo').each(function() {
var newS = $(this).val();
tArray = newS;
optVal.splice(0, 0, tArray);
});
Please help!
Here is my Fiddle: https://jsfiddle.net/rprakash/e1xcd2gh/
.push()? It seems like you are using an event handler, please show us in the code and provide a runnable example. I cannot get those results with your code.