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

  • Home
  • SEARCH
  • 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 8976413
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 15, 20262026-06-15T19:07:37+00:00 2026-06-15T19:07:37+00:00

I have a select list that looks like this: <select id=testSelect> <option value=option1 label=1>Option

  • 0

I have a select list that looks like this:

<select id="testSelect">
    <option value="option1" label="1">Option 1</option>
    <option value="option3" label="3">Option 3</option>
    <option value="option5" label="5">Option 5</option>
</select>​

I’m using the label to control the order in which the options should be displayed. The initial select list is in the correct order (despite missing items).

I would like to programatically add a new option to this select list. I found this question which provided me with a link to an excellent jquery select cheat sheet.

This cheat sheet provides the following code to add options at a specific index, which is similar to what I want to do:

$("#elementid option:eq(0)").after("<option value='4'>Some pears</option>"

However, I don’t need to insert a new option after a specific index (because these items will be removed and re-added multiple times), I need to insert the option in the correct order based on the label value.

I tried two things:

$(function () {
   $("#testSelect option:eq(1)").after("<option value='option2' label='2'>Option 2</option>"); //Method A
    $("#testSelect option:label(1)").after("<option value='option2' label='2'>Option 2</option>"); //Method B
});​

Method A inserts the new item after the second item in the list, which makes my list look like: 1 3 2 5

Method B throws an exception: Uncaught Error: Syntax error, unrecognized expression: unsupported pseudo: label

However, even if Method B worked, I would still have an issue since the option with the label of 1 may not actually exist. What I’d really like to do is insert the new option in the correct place in the list (based on the label) regardless of whether or not specific option elements exist around it.

Any ideas how this could be done? jsfiddle

  • 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-15T19:07:39+00:00Added an answer on June 15, 2026 at 7:07 pm

    How about this?

    jsFiddle example

    You can insert your new options anywhere and then just sort the options based on their label value.

    $("#testSelect").append("<option value='option2' label='2'>Option 2</option>");
    
    $("#testSelect option").sort(function(a, b) {
        if ($(a).attr('label') < $(b).attr('label')) return -1;
        if ($(a).attr('label') > $(b).attr('label')) return 1;
        return 0;
    }).appendTo('#testSelect');​
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a select menu that looks like this: <select id ="cal-category-select"> <option value="/event-sort/list/2009/9/sports/">Sports
If i have a dropdown list that looks like this: <select id=select> <option value=Armoire>Armoire</option>
I have a list of options that looks like this: <select id=input_13> <option class=level-1
I have been asked to produce a select list that looks like this: The
I have a drop down list's select tag that looks like this: <select name=MyName
I have a query that looks like this: SELECT last_name, first_name, middle_initial FROM names
I have a hibernate call in my DAO that looks like this List<Associate> associate
I have list of MyObject that looks like this: public class MyObject{ public int
I have an object that looks like this. List<List<string>> MyData; The inner List<string> can
I have one List of objects that looks somthing like this class Item {

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.