Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people, just like you, are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
Solved

how to sort drop down values

Posted on 2014-12-09
3
75 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 143

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

Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
$q.all is slower or not  in  Angularjs 2 21
Add or delete table rows 10 53
jQuery dialog box help 2 17
JS Event Does not Trigger From File 2 26
What is a Lightbox? A Lightbox is the effect you see when you click, for example, an image and the screen fades out and up pops the same image but in its full size dimensions. There are lots of Lightbox effects for jQuery. Problem is they are a…
This article discusses how to create an extensible mechanism for linked drop downs.
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)
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…

839 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