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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 13, 20262026-05-13T20:01:41+00:00 2026-05-13T20:01:41+00:00

For some reason, I can’t get this to work. My options list is populated

  • 0

For some reason, I can’t get this to work.

My options list is populated dynamically using these scripts:

function addOption(selectId, value, text, selected) {
    var html = '<option value="'+value+'">'+text+'</option>';
    if (selected == "on") {
        html = '<option value="'+value+'" selected="selected">'+text+'</option>';
    }
    $('#'+selectId).append(html);
}

function addSalespersonOption(id, name, defsales) {
    addOption('salesperson', id, name, defsales);
}

Here is the HTML:

<td class="text-r"><label for="salesperson">Salesperson:</label></td>
<td>
    <select id="salesperson">
        <option value="">(select)</option>
    </select>
</td>

So far, the output is:

<option value="1266852143634" selected="selected">Eric Hunt</option>

The DOM shows this:

index              2
disabled           false
value              "1266852143634"
text               "Eric Hunt"
selected           false
defaultSelected    true

But for some reason, when the page is loaded, the dropdown does not display Eric Hunt as pre selected. Nor is anything for that matter.

How can I get “selected true” instead of “defaultSelected true”?


EDIT: As it turns out, the above code works perfectly, thanks to the help of deceze and rosscj2533‘s answers from below. The only reason it’s not working for me is, I found Ruby code that was overwriting the select elements.

Thanks for everyone’s help on this,

Cheers

  • 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-13T20:01:42+00:00Added an answer on May 13, 2026 at 8:01 pm

    The defaultSelected attribute is not settable, it’s just for informational purposes:

    Quote:

    The defaultSelected property returns the default value of the selected attribute.
    This property returns true if an option is selected by default, otherwise it returns false.

    I think you want:

    $('option[value=valueToSelect]', newOption).attr('selected', 'selected');
    

    I.e. set the selected attribute of the option you want to select.


    Without trying to fix your code, here’s roughly how I would do it:

    function buildSelect(options, default) {
        // assume options = { value1 : 'Name 1', value2 : 'Name 2', ... }
        //        default = 'value1'
    
        var $select = $('<select></select>');
        var $option;
    
        for (var val in options) {
            $option = $('<option value="' + val + '">' + options[val] + '</option>');
            if (val == default) {
                $option.attr('selected', 'selected');
            }
            $select.append($option);
        }
    
        return $select;
    }
    

    You seem to have a lot of baggage and dependencies already and I can’t tell you how to best integrate the selected option into your code without seeing more of it, but hopefully this helps.

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

Sidebar

Related Questions

For some reason I can get this to work, using single proxy everything seems
Relatively new to using database and for some reason I can't get this 'execute'
For some reason I can't get this to work at all. I have read
For some reason I can't get Basic Authentication to work using PHP on my
For some reason I can't get my SVG filters to work in Firefox. They
For some reason I can't seem to get the MySQL Net Connector to work
For some reason i can't seem to get this right ok i have 2
For some reason I can't get static_dir to work. In my app.ymal I have:
This should be pretty simple but for some reason I can't get this value
I have this code. For some reason I can't get the contentpresenter to stretch

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.