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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T07:39:34+00:00 2026-05-27T07:39:34+00:00

I have a form UI whereby several sections require duplicate HTML select list to

  • 0

I have a form UI whereby several sections require duplicate HTML select list to be updated dynamically from a single, dynamically-updatable select list.

The dynamically-updatable list works just fine, in that new options can be added and removed on-the-fly. I can then get this update to propagate through each of the duplicate lists using JQuery .find(). I have even added a bit of logic to maintain the currently selected index of the original select list.

What I’m not able to do is maintain the selected state of each of the duplicate select lists as new options are added and removed from the original select list. As each update to the original select list iterates through each duplicate select list, they lose their currently selected option index.

Here is an example of my conundrum–*EDIT–I would encourage you to try and execute the code I’ve provided below and apply your theories before suggesting a solution, as none of the suggestions so far have worked. I believe you will find this problem a good deal trickier than you might assume at first:

<form>
<div id="duplicates">
<!--// I need for each of these duplicates to maintain their currently selected option index as the original updates dynamically //-->
  <select>
  </select>
  <select>
  </select>
  <select>
  </select>
</div>
<div>
  <input type="button" value="add/copy" onclick="var original_select = document.getElementById('original'); var new_option = document.createElement('option'); new_option.text = 'Option #' + original_select.length; new_option.value = new_option.text; document.getElementById('original').add(new_option); original_select.options[original_select.options.length-1].selected = 'selected'; updateDuplicates();" />
  <input type="button" value="remove" onclick="var original_select = document.getElementById('original'); var current_selected = original_select.selectedIndex; original_select.remove(original_select[current_selected]); if(original_select.options.length){original_select.options[current_selected < original_select.options.length?current_selected:current_selected - 1].selected = 'selected';} updateDuplicates();" />
  <select id="original">
  </select>
</div>
<script type="text/javascript" src="http://code.jquery.com/jquery-latest.min.js"></script>
<script type="text/javascript">
    function updateDuplicates(){
        $("#duplicates").find("select").html($("#original").html());
     }
</script>
</form>

It is important to note that the duplicate HTML select lists should remain somewhat arbitrary, if at all possible (i.e.; no ID’s) as this method needs to apply generically to other dynamically-created select lists throughout the document.

Thanks in advance!

  • 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-27T07:39:35+00:00Added an answer on May 27, 2026 at 7:39 am

    Still not 100% sure what you’re asking but it seems like this should do what you’re looking for and is a few less lines of code.

    (function () {
            function updateDuplicates() {
                $("#duplicates").find("select").html($("#original").html());
    
                $('#duplicates select').each(function () {
                    var lastSelectedValue = $(this).data('lastSelectedValue');
                    $(this).val(lastSelectedValue || $(this).val());
                });
            }
    
            $(document).ready(function () {
                $('button:contains(remove)').bind('click', function (e) {
                    e.preventDefault();
                    var original_select = document.getElementById('original'),
                    current_selected = original_select.selectedIndex;
    
                    original_select.remove(original_select[current_selected]);
                    if (original_select.options.length) {
                        original_select.options[current_selected < original_select.options.length ? current_selected : current_selected - 1].selected = 'selected';
                    }
                    updateDuplicates();
                });
    
                $('button:contains(add/copy)').bind('click', function (e) {
                    e.preventDefault();
                    var original_select = document.getElementById('original'),
                    new_option = document.createElement('option');
    
                    new_option.text = 'Option #' + original_select.length;
                    new_option.value = new_option.text;
                    document.getElementById('original').add(new_option);
                    original_select.options[original_select.options.length - 1].selected = 'selected';
                    updateDuplicates();
                });
    
                $('#duplicates select').bind('change', function () {
                    $(this).data('lastSelectedValue', $(this).val());
                });
            } ());
        } ());
    

    EDIT: I changed your markup to be

    <button>add/copy</button>
    <button>remove</button>
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have an application whereby I dynamically create controls on a form from a
Learning C# here. I have a monthly process form whereby one can select to
I have form that displays several keywords (standard set of choice lists that changes
I have a feature whereby a class in a dll displays a form asking
I am coding in C# using Visual Studio 2010 and have a form whereby
I have a dilemma whereby I have a form which contains a number of
I have form in my html page <form id=login_form method=POST action=index.php> <table> <tr> <td><label
I have a Java application where reports are generated dynamically in the form of
I have a form showing a gridview. The data is bound dynamically, depending on
I have a contact us form, whereby the user enters his name,email address and

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.