Solved

how to sort drop down values

Posted on 2014-12-09
3
72 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
Comment Utility
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]
Comment Utility
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
Comment Utility
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

Highfive + Dolby Voice = No More Audio Complaints!

Poor audio quality is one of the top reasons people don’t use video conferencing. Get the crispest, clearest audio powered by Dolby Voice in every meeting. Highfive and Dolby Voice deliver the best video conferencing and audio experience for every meeting and every room.

Join & Write a Comment

Suggested Solutions

Introduction Chart.js, used properly, can visually add a difference to your charting applications. It engages your visitors and allows them to interact with data they otherwise wouldn't be able to without expensive and complicated systems. For this…
Introduction Knockoutjs (Knockout) is a JavaScript framework (Model View ViewModel or MVVM framework).   The main ideology behind Knockout is to control from JavaScript how a page looks whilst creating an engaging user experience in the least …
The viewer will learn how to dynamically set the form action using jQuery.
The viewer will learn the basics of jQuery including how to code hide show and toggles. 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…

772 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

10 Experts available now in Live!

Get 1:1 Help Now