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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 15, 20262026-06-15T01:41:20+00:00 2026-06-15T01:41:20+00:00

I am using a select box to clone a div (based on numerical selection)

  • 0

I am using a select box to clone a div (based on numerical selection) and increment the IDs and labels of the new divs. I have this working but my problem is that if you select a number larger than 2 the divs do not increment in order. They do if you select 2 then 3 then 4 etc.

Basically, if you select 3 in the select box I want the result to be:

Membership Program for Adult #1
Membership Program for Adult #2
Membership Program for Adult #3

Rather than:

Membership Program for Adult #1
Membership Program for Adult #3
Membership Program for Adult #2

Here Is my JS:

$(document).ready(function () {
    //onchange of rooms-count
    $('#num-adults').change(function() {
        var roomsSelected = $('#num-adults option:selected').val();
        var roomsDisplayed = $('[id^="program-"]:visible').length;
        var roomsRendered = $('[id^="program-"]').length;

        if (roomsSelected > 0) {
            $('.membership-programs.adult label').show();
        }
        else {
            $('.membership-programs.adult label').hide(); 
        }

        //if room count is greater than number displayed - add or show accordingly
        if (roomsSelected > roomsDisplayed) {
            for (var i=1;i<=roomsSelected;i++){
                var r = $('#program-' + i);
                if (r.length == 0) {
                    var clone = $('#program-1').clone(); //clone
                    clone.children(':first').text("Membership Program for Adult #"+i);

                    //change ids appropriately
                    setNewID(clone, i);
                    clone.children('div').children('select').each(function() {
                        setNewID($(this),i);
                    });
                    $(clone).insertAfter($('#program-'+roomsRendered));
                }
                else {
                    //if the room exists and is hidden 
                    $(r).show();
                }
            }
        }
        else {
            //else if less than room count selected - hide
            for (var i=++roomsSelected;i<=roomsRendered;i++){
                $('#program-'+i).hide();
            }
        }
    });

    function setNewID(elem, i) {
        oldID = elem.attr('id');
        newId = oldID.substring(0, oldID.indexOf('-')) + "-" + i;
        elem.attr('id',newId);
    }
});​

Here Is my HTML:

<div id="hotel-guests">
    <span class="wrapper">
        <label for="num-adults"># of Adults:</label>
        <select tabindex="4" name="amtrakadults" id="num-adults" class="traveler-dropdown">
            <option value="0">0</option>
            <option value="1">1</option>
            <option value="2">2</option>
            <option value="3">3</option>
            <option value="4">4</option>
            <option value="5">5</option>
            <option value="6">6</option>
            <option value="7">7</option>
            <option value="8">8</option>
            <option value="9">9</option>
        </select>
    </span>    
</div>
<div class="membership-programs adult">
    <div id="program-1" class="program">
        <span>Membership Program for Adult #1:</span>
        <span class="wrapper">
            <select tabindex="4" name="program" class="membership-program">
                <option value="None">None</option>
                <option value="AAA">AAA</option>
                <option value="Student Advantage">Student Advantage</option>
                <option value="Veterans Advantage">Veterans Advantage</option>
                <option value="NARP">NARP</option>
                <option value="ISIC">ISIC</option>
            </select>
        </span>
    </div>
</div>​

Here is a fiddle displaying the problem.

  • 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-15T01:41:22+00:00Added an answer on June 15, 2026 at 1:41 am

    Change this:

    $(clone).insertAfter($('#program-'+roomsRendered));
    

    To this:

    $(clone).appendTo('.membership-programs');
    

    Updated fiddle

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

Sidebar

Related Questions

I am creating a select box for a form using this in _form.html.erb <%=
I am using chosen.js ( http://harvesthq.github.com/chosen/ ) and I have a select box that
I'm using this plugin - http://plugins.jquery.com/project/styledSelect to style a Select box on my page.
I recreated a Select box and its dropdown function using this: $(.selectBox).click(function(e) { if
I'm populating my select box using the query shown below: <%=f.select :acoustic, options_for_select(User.includes(:roles).where( :roles
I am implementing a function populatig a select box using data from another select
I am working on select box and using jquery. HTML structure: <form method= action=
How to make an option in select box readonly not disabled using jquery? I
I'm using CakePHP 2.0. Is it possible to display multiple fields in select/dropdown box
I have two files in use. The first is a front end select box

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.