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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 7, 20262026-06-07T16:41:55+00:00 2026-06-07T16:41:55+00:00

I am trying to change the selected option for a group of html select

  • 0

I am trying to change the selected option for a group of html select elements. I have given them all the same class so that I could use jQuery to select them all at once, but for some reason it only changes the first element in the class. Here is a demo: http://jsfiddle.net/bwhitney/xP4FP/1/

Here is just the code:

<select id="config1" class="config">
<option>foo</option>
<option selected>bar</option>
</select>

<select id="config2" class="config">
<option>foo</option>
<option selected>bar</option>
</select>

<select id="config3" class="config">
<option>foo</option>
<option selected>bar</option>
</select>

With the jQuery:

$('.config option:eq(0)').attr('selected', 'selected');​

The result of this code is that only the first select element will have the foo option selected. The second and third will still have bar selected. I thought that using jQuery to select a class would select all items having that class. Is there some way to select all three of these with one selector?

As a pre-emptive response to a likely answer (I’m sure someone will think to suggest this):
I know that I can just write a for loop to select each $('#config' + i). This is what I will end up doing if there is no way to just select them all at once.

  • 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-07T16:41:57+00:00Added an answer on June 7, 2026 at 4:41 pm

    Try using nth-child:

    $('select option:nth-child(1)').prop('selected', 'selected');
    

    Example: http://jsfiddle.net/K4mnx/

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

Sidebar

Related Questions

I am trying to change the selected option in a select dropdown box with
Using jQuery, I'm trying to change the selected option in a SELECT element to
I'm trying to change the currently selected option in a select with the Chosen
Given the following HTML selects: <select id=HtmlSelect1> <option value=1 selected=selected>1</option> <option value=2>2</option> <option value=3>3</option>
I'm trying to run the following code $(select#shipping_rates_drop).change(function(){ var selectedText = $(this + option:selected).text();
I'm trying to get the selected option via jQuery everytime I change it, and
I have two select with the same elements (options) in it. What I am
Using JQuery, I'm trying to set an option as selected in a 'select' element
I have a select menu that i'm trying to bind a click function and
I'm trying to fire a change event for a selected group radio button but

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.