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 3441440
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 18, 20262026-05-18T08:33:12+00:00 2026-05-18T08:33:12+00:00

I am generating a select list with option elements in jQuery. Simplified it looks

  • 0

I am generating a select list with option elements in jQuery. Simplified it looks like this:

var sel = $("<select>");
$.each(data, function(i, v){
    $("<option>").attr({ "value": i }).html(v).appendTo(sel);
});

Then I want an option to be selected. Normally I would do something like:

$(sel).val(1);

But then the option does not have the selected attribute present.

$(sel).html() gives something like:

<option value="1">1</option>
<option value="2">2</option>

and this is what I expected:

<option value="1" selected="selected">1</option>
<option value="2">2</option>

I tried this approach (same result as using .val()):

$.each(data, function(i, v){
    var attrs = { "value": i };
    if(i == 1){
        attrs.selected = "selected";
    }
    $("<option>").attr(attrs).html(v).appendTo(sel);
});

but the only way I found working is:

$.each(data, function(i, v){
    var el = "<option>";
    if(i == 1){
        el = '<option selected="selected"></option>';
    }
    $(el).attr({ "value": i }).html(v).appendTo(sel);
});

Is there any other or better way?

  • 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-05-18T08:33:13+00:00Added an answer on May 18, 2026 at 8:33 am

    You could use the native setAttribute() method. I believe jQuery muddles the distinction between attributes and properties.

    This gets the <option> you want by its index from the <select> element’s options collection, and sets the attribute:

    sel[0].options[1].setAttribute('selected','selected');
    

    Or this uses jQuery to set the value first, then goes back and gets the correct option using the native selectedIndex property.

    sel.val(1);
    sel[0].options[sel[0].selectedIndex].setAttribute('selected','selected');
    

    EDIT: Off topic a little, but you could create the <option> elements a little differently like this:

    $("<option>",{
        value: i,
        html: v
    }).appendTo(sel);
    

    Since jQuery 1.4, you can send a collection of values you wish to set for a new element.

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

Sidebar

Related Questions

Ok, this is a weird request, but I need to use jQuery to manipulate
What is an efficient way of generating N unique numbers within a given range
I am pre-generating short codes for a service I am building that shortens links.
I have a MySQL table , each row representing a Person. For each person
I'm making a XHTML Page, and i'm using php to generate it. So, now
I'm making a start on an MVC project, having gone through the MvcMusicStore tutorial.
Let's say that I got the following class and enum: public class MyModel {
I am developing the ASP.NET MVC 2 application . I want to show the
Here's what i'm trying to do: 1 textbox with an Add button, and a

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.