Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8622437
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 12, 20262026-06-12T07:03:12+00:00 2026-06-12T07:03:12+00:00

I’ve got two dropdowns, as a result of changing the first one it triggers

  • 0

I’ve got two dropdowns, as a result of changing the first one it triggers a jquery change() that does a post with the company id from the first dropdown. That then runs a sql query to get a list of people that work for that company and fills the select box with options. This is working fine and I’ve got the sql query set to ORDER BY admin_name. But when jquery starts inserting the options into the dropdown it appears to be sorting by the option value instead (admin_id). What can I do to keep the options ordered by the admin_name (the text of the option). Below is the jQuery code responsible for adding the options:

$.post("listSignedBy.php", { company_id: company_id },
                function(data) {
                        alert(data); <-- this shows that the data is sorted correctly by the admin name.
                        var select = $('#signed_by');
                        if(select.prop) {
                                  var options = select.prop('options');
                        }
                        else {
                                  var options = select.attr('options');
                        }
                        $('option', select).remove();

                        var obj = jQuery.parseJSON(data);
                                    options[options.length] = new Option('-- Select Signed By --', '');
                        $.each(obj, function(val, text) {
                                    options[options.length] = new Option(text, val);
                        });
                        select.val(selectedOption);

                });  

Thank you for any assistance and please let me know if you need any further information to help troubleshoot/fix.

As requested, example JSON data:

{"19082":"Aaron Smith","19081":"Becky Doe"}

So in this case what I WANT is:

<option value='19082'>Aaron Smith</option>
<option value='19081'>Becky Doe</option>

But instead of sorting by the text, it’s sorting by the value so I get:

<option value='19081'>Becky Doe</option>
<option value='19082'>Aaron Smith</option>
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-12T07:03:13+00:00Added an answer on June 12, 2026 at 7:03 am

    The object is not sorted.. It is actually getting inserted in the order how it is iterated.

    However, in case if the order of returned response is not in the desired order then the best way is it insert the options first and sort the options. See below,

    DEMO: http://jsfiddle.net/gGd82/3/

    // convert OPTIONs NodeList to an Array
    // - keep in mind that we're using the original OPTION objects
    var ary = (function(nl) {
        var a = [];
        for (var i = 0, len = nl.length; i < len; i++) {
          a.push(nl.item(i));
        }
        return a;
    })(options);
    // sort OPTIONs Array
    ary.sort(function(a, b) {
        return a.text < b.text ? -1 : a.text > b.text ? 1 : 0;    
    });
    // remove all OPTIONs from SELECT (don't worry, the original
    // OPTION objects are still referenced in "ary") ;-)
    options.length = 0;
    
    // (re)add re-ordered OPTIONs to SELECT
    select.html(ary);
    
    select.prepend($('<option />').text('-- Select Signed By --').val(''));
    

    References: Sorting dropdown list using Javascript

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I've got a string that has curly quotes in it. I'd like to replace
I am reading a book about Javascript and jQuery and using one of the
I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
I'm trying to create an if statement in PHP that prevents a single post
I'm making a simple page using Google Maps API 3. My first. One marker
link Im having trouble converting the html entites into html characters, (&# 8217;) i
That's pretty much it. I'm using Nokogiri to scrape a web page what has
I have a jquery bug and I've been looking for hours now, I can't
I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
I have a French site that I want to parse, but am running into

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.