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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 31, 20262026-05-31T11:53:21+00:00 2026-05-31T11:53:21+00:00

I have managed to get my click event working with the following code. When

  • 0

I have managed to get my click event working with the following code. When I click the button I want mutiple fields to appear. For example at the moment when I click ‘add another address only the last field clones but I want all fields to clone e.g. street, line2, line3 etc. I no I need to add more code in the jquery but not exactly sure what!
Thanks in advance

Script:

$(document).ready(function() {
    $('#btnAdd').click(function() {
        var num = $('.clonedInput').length;
        var newNum = new Number(num + 1);
        var newElem = $('#input' + num).clone().attr('id', 'input' + newNum);
        newElem.children(':first').attr('id', 'name' + newNum).attr('name', 'name' + newNum);
        $('#input' + num).after(newElem);
        $('#btnDel').attr('disabled', '');
        if (newNum == 3) $('#btnAdd').attr('disabled', 'disabled');
    });
    $('#btnDel').click(function() {
        var num = $('.clonedInput').length;
        $('#input' + num).remove();
        $('#btnAdd').attr('disabled', '');
        if (num - 1 == 1) $('#btnDel').attr('disabled', 'disabled');
    });
    $('#btnDel').attr('disabled', 'disabled');
});​

Sample of html form:

Street*

<div id="input2" style="margin-bottom:4px;" class="clonedInput">
Line2<span class="required"><font color="#CC0000">*</font></span> 
<input name="Line2"  type="text" id="Line2"> 
</div>

<div id="input3" style="margin-bottom:4px;" class="clonedInput">
Line3<span class="required"><font color="#CC0000">*</font></span> 
<input name="Line3"  type="text" id="Line3"> 
</div>

<div id="input4" style="margin-bottom:4px;" class="clonedInput">
Town<span class="required"><font color="#CC0000">*</font></span>   
<input name="Town"  type="text" id="Town">
</div>

<div id="input5" style="margin-bottom:4px;" class="clonedInput">
Postcode<span class="required"><font color="#CC0000">*</font></span> 
<input name="Postcode"  type="text" id="Postcode">
</div>

For example when using the codes above only the Postcode field would double. My main aim from this is that applicants can add more than one address.
Thanks

  • 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-31T11:53:22+00:00Added an answer on May 31, 2026 at 11:53 am

    In your code you are just cloning the last element. I would suggest you to wrap the whole address inside a div wrapper and clone the wrapper instead of cloning each and every element. Then you can work through the cloned object. Try my demo and check the output HTML to see how it works.

    DEMO

    Below is the actual code,

    JS:

    $(document).ready(function() {
        $('#btnAdd').click(function() {
            var $address = $('#address');
            var num = $('.clonedAddress').length;
            var newNum = new Number(num + 1);
            var newElem = $address.clone().attr('id', 'address' + newNum).addClass('clonedAddress');
    
            //set all div id's and the input id's
            newElem.children('div').each(function(i) {
                this.id = 'input' + (newNum * 5 + i);
            });
            newElem.find('input').each(function() {
                this.id = this.id + newNum;
                this.name = this.name + newNum;
            });
    
            if (num > 0) {
                $('.clonedAddress:last').after(newElem);
            } else {
                $address.after(newElem);
            }
    
            $('#btnDel').removeAttr('disabled');
    
            if (newNum == 3) $('#btnAdd').attr('disabled', 'disabled');
        });
    
        $('#btnDel').click(function() {
            $('.clonedAddress:last').remove();
            $('#btnAdd').removeAttr('disabled');
            if ($('.clonedAddress').length == 0) {
                $('#btnDel').attr('disabled', 'disabled');
            }
        });
        $('#btnDel').attr('disabled', 'disabled');
    });
    

    HTML:

    <div id="address">
      <div id="input1" style="margin-bottom:4px;" class="input">
        Street<span class="required">*</span>
        <input name="Street"  type="text" id="Street">
      </div>
      <div id="input2" style="margin-bottom:4px;" class="input">
       Line2<span class="required">*</span>
       <input name="Line2"  type="text" id="Line2">
      </div>
      <div id="input3" style="margin-bottom:4px;" class="input">
       Line3<span class="required">*</span>
       <input name="Line3"  type="text" id="Line3">
      </div>
      <div id="input4" style="margin-bottom:4px;" class="input">
       Town<span class="required"><font color="#CC0000">*</font></span>   
       <input name="Town"  type="text" id="Town">
      </div>
      <div id="input5" style="margin-bottom:4px;" class="input">
       Postcode<span class="required">*</span>
       <input name="Postcode"  type="text" id="Postcode">
      </div>
    </div>
    

    NOTE: Removed font tag from html and added below css for the required *

    CSS:

    .required { color: #cc0000; }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Have you managed to get Aptana Studio debugging to work? I tried following this,
I have finally managed to get javan-whenever gem working on my site5 server, and
I want to click a button, have a text input box show up and
I have a button with a click event (from a 3. party library) which
I have managed to get a cron job to run a rake task by
Ok so I have managed to get the format of the date presented in
I have successfully managed to get System.Speech.Synthesis to read English text in arbitrary voices
I asked before about pixel-pushing, and have now managed to get far enough to
I managed to get some help from a previous question and I have the
I have a media program. i am adding CD,dvd,book info. I managed to get

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.