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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 2, 20262026-06-02T18:38:06+00:00 2026-06-02T18:38:06+00:00

I am dynamically setting the options of my select-fields. Every time the form changes,

  • 0

I am dynamically setting the options of my select-fields. Every time the form changes, new data is received from a Servlet and the current selections are overwritten.

Problem here is, that if I select an option, the form loads the javascript function, and I loose my selection / focus.

var capacityOptionsAsString = "";
for(var i = 0; i < capacityArray.length; i++) {
  capacityOptionsAsString += "<option value='" + capacityArray[i] + "'>" + capacityArray[i] + "</option>";
  console.log('capacity option: ' + capacityOptionsAsString);
}
$("select[name='inptCapacity']").find('option').remove().end().append($(capacityOptionsAsString));

Any idea how to keep the selection?


Edit: Maybe I was not clear enough. I want to keep the selection. So it has to be validated, if the option is selected, if it is, we have to select it again.

  • 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-02T18:38:09+00:00Added an answer on June 2, 2026 at 6:38 pm

    Just set the focus again:

    $("select[name='inptCapacity']").focus();
    

    Update

    get selected option:

    var selected_value = $("select[name='inptCapacity'] option:selected").val()
    

    Do your changes

    $("select[name='inptCapacity']").find('option').remove().end().append($(capacityOptionsAsString));
    

    Select it again:

    $('select[name='inptCapacity'] option[value="' + selected_value + "]').attr('selected', 'selected');
    

    You totally need an id for your select 😉

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

Sidebar

Related Questions

I'm creating a page that has some dynamically generated images built from data that
I have built a form using 3 select boxes that are used dynamically together,
Got a rails application that has a form (new action) with a select with
I have a class that requires dynamically setting a List. I also call this
My project requires me to allocate memory dynamically. What am I doing wrong? /*Setting
I am dynamically adding labels to a form and I would like to set
This code attempts to dynamically switch the class of the StateContainer div from StateOne
I have 2 fields for sorting: Product Name and Price plus 2 options: ascending
I am trying to dynamically load my authentication server type based on a setting.
I'm setting up a form in which multiple entries can potentially be made (via

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.