Solved

how to sort drop down values

Posted on 2014-12-09
3
73 Views
Last Modified: 2014-12-14
Hi i have drop down value


like <select name="x" id="x">
                <option selected="selected">Sathish</option>
             <option >vijay</option>
              <option >Fox</option>
                 <option >Ajith</option>
 <option >David</option>
</selected>

how to sort this drop down ?
0
Comment
3 Comments
 
LVL 25

Accepted Solution

by:
Lee Savidge earned 500 total points
ID: 40488658
Well, if you're using jQuery, then you can use one of the multitude of examples on the web. I've used this one here:

http://codepen.io/AlexBezuska/pen/kCwvJ

var sortSelect = function (select, attr, order) {
    if(attr === 'text'){
        if(order === 'asc'){
            $(select).html($(select).children('option').sort(function (x, y) {
                return $(x).text().toUpperCase() < $(y).text().toUpperCase() ? -1 : 1;
            }));
            $(select).get(0).selectedIndex = 0;
            e.preventDefault();
        }// end asc
        if(order === 'desc'){
            $(select).html($(select).children('option').sort(function (y, x) {
                return $(x).text().toUpperCase() < $(y).text().toUpperCase() ? -1 : 1;
            }));
            $(select).get(0).selectedIndex = 0;
            e.preventDefault();
        }// end desc
    }

};

//, attr, order
$(document).ready(function () {

    $('#btnSort').click(function (e) {

        sortSelect('#x', 'text', 'asc');

    }); // event listener click

});

Open in new window

0
 
LVL 142

Expert Comment

by:Guy Hengel [angelIII / a3]
ID: 40488660
unless the data comes from a db query and you build the list dynamically, the only way to sort is to sort them in the html code as such. maybe you could find some javascript to do this "after the page loaded", but I would highly recommend against such a technique...
0
 
LVL 30

Expert Comment

by:Alexandre Simões
ID: 40488869
If you use underscorejs you can do it with as little code as this:
var options = $('#x option');
var sorted = _.sortBy(options, function(item){
  return item.innerHTML;
});
$('#x').html(sorted);

Open in new window


Here's a working demo: http://plnkr.co/edit/re5RKeN2UcRAf7WRuxa5?p=preview

Although if you have the option, get it sorted right out of the database.
0

Featured Post

3 Use Cases for Connected Systems

Our Dev teams are like yours. They’re continually cranking out code for new features/bugs fixes, testing, deploying, testing some more, responding to production monitoring events and more. It’s complex. So, we thought you’d like to see what’s working for us.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
rhino JavaScript import, load 25 66
Add Context Menu Item 4 43
Need to click button twice to get results 2 32
Message not shown 5 30
Introduction If you're like most people, you have occasionally made a typographical error when you're entering information into an online form.  And to your consternation, the browser remembers the error, and offers to autocomplete your future entr…
Boost your ability to deliver ambitious and competitive web apps by choosing the right JavaScript framework to best suit your project’s needs.
The viewer will learn how to dynamically set the form action using jQuery.
The viewer will learn the basics of jQuery, including how to invoke it on a web page. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery.: (CODE)

912 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

15 Experts available now in Live!

Get 1:1 Help Now