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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 16, 20262026-05-16T10:36:19+00:00 2026-05-16T10:36:19+00:00

I know there are multiple ways to select a particular option from a using

  • 0

I know there are multiple ways to select a particular option from a using jQuery:

  1. $select.val("someValue")
  2. $option.attr("selected", "selected")
  3. $select[0].selectedIndex = 1

Assuming you have both the index of the option you wish to be selected and its value, what’s the most correct way (from the above, or otherwise) to make it selected?

By “most correct”, I mean:

  1. Best practice, if any
  2. Will correctly set the value so that it is submitted with the form, and can be retrieved using any of the above methods
  3. Any other reason why I’d choose one method over others
  • 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-16T10:36:19+00:00Added an answer on May 16, 2026 at 10:36 am

    $select.val(“someValue”)

    That’s fine, in the common case where it’s a non-multiple select and you don’t have two different <option>s with the same value. If that’s the case I’d go with this, as the most readable option.

    $select[0].selectedIndex = 1

    That’s more direct, so marginally quicker, and is necessary to be unambiguous for the case where you have multiple options with the same value.

    If you might have a multiple-select, you have to get/set the selectedness of each option separately:

    $select[0].options[1].selected= true;
    

    However:

    $option.attr(“selected”, “selected”)

    Not generally the best approach. The problem with attr() is it’s a nasty hack to access DOM properties and HTML attributes as if they are the same thing, trying to hide the difference from you. What attr() will do in this case is to set the selected DOM property, which is a boolean value. So attr('selected', true) would make more sense; 'selected' as a string value does also work, but only because all non-empty string values are ‘truthy’ in JavaScript.

    If you were actually setting the HTML selected attribute here, it would not have an effect on the selectedness of the option, because the selected attribute actually maps to the defaultSelected property and not selected! The selected property reflects the runtime form contents as altered by the user; defaultSelected reflects the actual attribute in the document containing the initial selectedness state.

    (Except on IE, due to a bug in its implementation of default values, and also in other browsers in some situations which are too intricate and confusing to go into. Take-home advice: don’t try setting the selected HTML attribute from script as the results may be unpredictable. Work with the DOM properties. The same is also true of value/defaultValue for inputs and checked/defaultChecked for checkbox/radio.)

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

Sidebar

Related Questions

I know a great thing about perl is that there's multiple ways of doing
As we all know, there are multiple ways of going about doing the same
I have an interesting little problem, and I know there are multiple ways to
I know there are multiple posts on this but I still can't get it
I know there are multiple solutions online, but some are for windows, some are
I'm making a small online game, where (what do you know) there'll be multiple
I know that there can be multiple values for an email, but I'm not
I would like to know if there are any ways one can perform 'locking'
Just a language feature question, I know there's plenty of ways to do this
I know there have been many questions on grid and pack in the past

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.