Skip to content
Advertisement

Multiple select get Selected options in order selected

i have a multiple select like the following

JavaScript

that i have implemented chosen plugin on i have an onchange listener that adds selected elements to an array like so

JavaScript

however in which ever option i select the items they always end up arranging themselves in the order they are in the multiple select for example by selecting the values Volvo , Audi, Saab in that order results in the following array

JavaScript

Is there a way that i can add elements in a multiple select to an array in the order which they were selected?

Advertisement

Answer

You always get the same order of the selected array because you create it each time you run your onChange function. So the solution would be to make it a global variable. When you do that, your array won’t be empty at the second choice tho, so you have to take care of removing items from it. Here’s some code

JavaScript
JavaScript
User contributions licensed under: CC BY-SA
5 People found this is helpful
Advertisement