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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 2, 20262026-06-02T07:42:35+00:00 2026-06-02T07:42:35+00:00

I am taking the amount from one select box and duplicating a div that

  • 0

I am taking the amount from one select box and duplicating a div that number of times. But I have run into a problem. I need the new divs (which contain select boxes) to change their name. I need it to add the incrementing number to the end of the select box name. Here is my JSFIDDLE: http://jsfiddle.net/liveandream/DhCLK/

JQUERY:

//NUMERATE SELECT
for (var i = 2; i < 5; i++) { // add counts in a for loop
    $('select[name="rooms"]').append('<option value=' + i + '>' + i + '</option>');
}
//#

//DUPLICATE PLUGIN
$.fn.duplicate = function(count, cloneEvents) {
    var tmp = [];
    for (var i = 0; i < count; i++) {
        $.merge(tmp, this.clone(cloneEvents).get());
    }
    return this.pushStack(tmp);
};

//SELECT CHANGE FUNCTION (on change get value and clone)
$('select[name="rooms"]').change(function(){  // on change...
    var numOfClones = $(this).val() -1;    // get value...
    $('#holder').html('');              // empty holder if there are some old clones
    $('.repeat').duplicate(numOfClones).addClass('new').appendTo('#holder')
     .find("select").each(function() {
                   this.name = this.name.replace(/\d+/, 2);
               });

// duplicate; fill holder with new clones; the class 'new' is just for styling
});
e.preventDefault();​

HTML:

<select name="rooms">
        <option>1</option>
</select>

<div class="repeat">
    <h4 style="font-size: 11px;">Adults (17+)</h4>
      <select name="adults">
        <option>1</option>
        <option>2</option>
        <option>3</option>
        <option>4</option>
       </select>
    <h4 style="font-size: 11px;">Children</h4>
        <select name="children" id="children">
        <option selected="selected">0</option>
        <option>1</option>
        <option>2</option>
        <option>3</option>
        </select>

        <h4 style="font-size: 11px;">Child AGES</h4>
        <select name="ages" id="ages" style="display: none;">
        <option selected="selected">-?-</option>
        <option>>0</option>
        <option>1</option>
        <option>2</option>
        <option>3</option>
        <option>4</option>
        <option>5</option>
        <option>6</option>
        <option>7</option>
        <option>8</option>
        <option>9</option>
        <option>10</option>
        <option>11</option>
        <option>12</option>
        <option>13</option>
        <option>14</option>
        <option>15</option>
        <option>16</option>
        <option>17</option>
        </select>
</div>

<div id="holder"></div>

I hope someone could help me with this… Thank you 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-06-02T07:42:37+00:00Added an answer on June 2, 2026 at 7:42 am

    You can change the clone code to add a number to the end of each select in each repeat div. You also need to fix the id values to be unique which I added to this code too. To get the same number added to each select within a given repeat div, I had to add another .each() loop so we could manually increment the counter at the right time.

    //SELECT CHANGE FUNCTION (on change get value and clone)
    $('select[name="rooms"]').change(function(){  // on change...
        var numOfClones = $(this).val() -1;    // get value...
        var cntr = 2;
        $('#holder').empty();              // empty holder if there are some old clones
        $('.repeat').duplicate(numOfClones).addClass('new').appendTo('#holder').each(function() {
            $(this).find("select").each(function() {
                if (this.name) {
                    this.name += cntr;
                }
                if (this.id) {
                    this.id += cntr;
                }
            });
            ++cntr;
        });
    // duplicate; fill holder with new clones; the class 'new' is just for styling
    });
    

    This code works because the master copy has no number at the end. If it did have a number at the end, then you could do a replace operation on that number.

    Working demo: http://jsfiddle.net/jfriend00/hhUaR/

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

Sidebar

Related Questions

I am taking the amount from one input box and creating a for...each loop
I have a dataframe in R that I loaded from a CSV file. One
I have a web application that runs multiple websites from one codebase. I have
I have a thread that needs to write data from an in-memory buffer to
Taking the jQuery framework for example, if you run code like this: $(document).ready(function init()
When taking a database from a relatively un-normalized form and normalizing it, what, if
I have a one-to-one relation where I use CascadeType.PERSIST. This has over time build
I have a C++ program that has a pretty terrible memory leak, about 4MB
This is the scenario i have: im developing a web app that will list
I have a java.sql.ResultSet object that I need to update. However the result set

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.