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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 14, 20262026-05-14T06:20:34+00:00 2026-05-14T06:20:34+00:00

I am looking for an effective way to either clone/rename or re-create address fields

  • 0

I am looking for an effective way to either clone/rename or re-create address fields to offer ability to submit multiple addresses on the same page. So with form example like this:

<div id="addresses">
  <div class="address">
    <input type="text" name="address[0].street">
    <input type="text" name="address[0].city">
    <input type="text" name="address[0].zip">
    <input type="text" name="address[0].state">
  </div>
</div>
<a href="" id="add_address">Add address form</a>

From what I can understand there are two options to do that:

  1. Recreate the form field by field and increment the index which is kind of verbose:

    var index = $(".address").length;
    
    $('<`input`>').attr({
    name: 'address[' + index + '].street',
    type: 'text'
    }).appendTo(...);
    
    $('<`input`>').attr({
    name: 'address[' + index + '].city',
    type: 'text'
    }).appendTo(...);
    
    $('<`input`>').attr({
    name: 'address[' + index + '].zip',
    type: 'text'
    }).appendTo(...);
    
    $('<`input`>').attr({
    name: 'address[' + index + '].state',
    type: 'text'
    }).appendTo(...);
    
  2. Clone Existing layer and replace the name in the clone:

    $("div.address").clone().appendTo($("#addresses"));
    

Which one do you recommend using in terms of being more efficient and if its #2 can you please suggest how I would go about search and replacing all occurrences of [0] with [1] ([n]). Thank you.

  • 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-14T06:20:34+00:00Added an answer on May 14, 2026 at 6:20 am

    In theory, the simplest way would be to clone then change name:

    var newaddress= $("#addresses div.address").eq(0).clone();
    newaddress.find('input').each(function() {
        this.name= this.name.replace('[0]', '['+i+']');
    });
    $('#addresses').append(newaddress);
    

    However:

    a. jQuery’s clone() is a really nasty piece of work. On IE, it serialises the nodes to HTML, processes the HTML string with regex (!) to remove its bodge-job internal ID attributes and then asks the browser to re-parse it. This is one of my least favourite parts of jQuery: it’s highly error-prone, loses some information, and is slow (not that speed matters for the quite small job you seem to be doing here).

    The browser’s native cloneNode(true) method is much better, but you can’t really use it if you’re doing jQuery stuff because it will copy jQuery’s internal ids, potentially confusing its scripting. Ugh. What a mess.

    b. When you change an input’s name, whether that’s by input.name as here or using attr() as in your example, there are issues in IE<=7.

    In particular, though it will submit the inputs with the correct control name, they won’t be indexed under the right name in the form.elements HTMLCollection (or on form itself, though you shouldn’t use that anyway). This isn’t necessarily a problem if you are selecting inputs based on IDs or jQuery selectors rather than the old-school HTMLCollection interface. Worse is that radio buttons, if you use any, won’t be properly name-grouped.

    If this is a concern for you I’m afraid using innerHTML/html() to create the div, as in pst’s answer, is your only option. (You can always combine the two, creating using HTML then changing the other attributes and text content using text(), attr(), val() etc. to avoid the usual HTML-escaping problems when you go around sticking text strings into HTML.)

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

Sidebar

Related Questions

I'm looking for an effective way to execute a method everyday at 3PM regardless
I'm looking for some way to effectively hide inherited members. I have a library
I'm looking for a cost effective tool for managing an web app on Ec2.
I am looking for easy steps that are simple and effective in making a
Im looking for ideas on how to effectively notify users that their input into
Looking for feedback on : http://code.google.com/p/google-perftools/wiki/GooglePerformanceTools
Looking for an example that: Launches an EXE Waits for the EXE to finish.
Looking for C# class which wraps calls to do the following: read and write
Looking at what's running and nothing jumps out. Thanks!
Looking to do a very small, quick 'n dirty side project. I like the

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.