Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I get the selected items from a multi-select listbox in order that they were selected?

I have web application in which i populate listbox using items from database on page load.

I get the all items from listbox that are selected but they are in order in which they populated.

I want these items to be in the order that they are selected by user.

I tried .change(function() using jQuery but it returns only first selected items value.

I attaching my code for reference. I have used listbox using http://harvesthq.github.com/chosen/

This is my listbox:

   <asp:ListBox ID="dr_menuitem" runat="server" class="chzn-select" SelectionMode="Multiple" style="width:300px;" >
                    <asp:ListItem></asp:ListItem>
               </asp:Listbox>

This is the jQuery I call:

<script type="text/javascript">
           $(document).ready(function() {

               $('#dr_menuitem').change(function() {

                     alert($("#dr_menuitem option:selected").val());
                   });
           });

    </script>
like image 249
sp_m Avatar asked Dec 05 '25 15:12

sp_m


1 Answers

To get the selected value in selected order try this:

$("#dr_menuitem").change(function() {

   // $(this).val() will give you an array
   // of selected value in order
   // you've selected

   alert( $(this).val() );
});

DEMO

like image 70
thecodeparadox Avatar answered Dec 07 '25 04:12

thecodeparadox



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!