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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 6, 20262026-06-06T07:03:22+00:00 2026-06-06T07:03:22+00:00

I am attempting to clone fields in a form using Jquery. I’m trying to

  • 0

I am attempting to clone fields in a form using Jquery.

I’m trying to achieve the following in terms of display:

SELECT     SELECT     TEXT INPUT
RADIO      RADIO      RADIO  

Clone 1
SELECT     SELECT     TEXT INPUT
RADIO      RADIO      RADIO

Clone2
SELECT     SELECT     TEXT INPUT
RADIO      RADIO      RADIO


What I end up getting is:
RADIO   RADIO    RADIO 
RADIO   RADIO    RADIO 
RADIO   RADIO    RADIO 
SELECT     SELECT     TEXT INPUT
SELECT     SELECT     TEXT INPUT
SELECT     SELECT     TEXT INPUT

In my code below, I have used the .after function to follow the Select options, but it still displays it above

$(‘#condition’ + num).after(newElem);
// insert the new element after the last “duplicatable” input field
$(‘#logical’ + num1).after(newElem);

A demo is on Jsfiddle http://jsfiddle.net/noscirre/ATzBA/

HTML

<table width="100%" cellspacing="4" cellpadding="5" border="0">
          <tbody><tr id="logical1" class="clonedInput1">
              <td class="first-column">&nbsp;</td>
              <td><input type="radio" name="logicalOperator" default>
                AND    &nbsp; &nbsp;
                <input type="radio" name="logicalOperator" >
                OR    &nbsp; &nbsp;
                <input type="radio" name="logicalOperator" >
                NOT
                </td>
            </tr>
            <tr id="condition1" class="clonedInput">
              <td class="first-column">Condition #2</td>
              <td><span style="float:left; padding-right: 10px;">
                <select id="firstname" name="firstname" class="standard_select" style="width:147px; background:#fff;">
                   <option>Blah Name</option>
                   <option>Blah list</option>
                   <option>Blah Type</option>
                   <option>Blah Id</option>
                   <option>Blah Name</option>

                </select>&nbsp;<select id="firstname" name="firstname" class="standard_select" style="width:147px;">
                  <option>Equals =</option>
                  <option>Not Equal &lt;&gt;</option>
                  <option>Greater &gt;</option>
                  <option>Less &lt;</option>
                  <option>Contains</option>
                  <option>In</option>
                  <option>Not In</option>
                </select>&nbsp;<input type="text" id="conValue" name="conValue" class="short_input" value="" style="width:147px;">
                </span>
                </td>
</tr>
<tr>
              <td class="first-column">&nbsp;</td>
              <td>
<div>
                    <input type="button" id="btnAdd" value="Add" />
                    <input type="button" id="btnDele" value="Del" />
                </div>    
                </td>
</tr></tbody>
</table>

JAVASCRIPT

​`            $('#btnAdd').click(function() {
                var num     = $('.clonedInput').length; // how many "duplicatable" input fields we currently have
                var num1     = $('.clonedInput1').length; // how many "duplicatable" input fields we currently have

                var newNum  = new Number(num + 1);      // the numeric ID of the new input field being added
                var newNum1  = new Number(num1 + 1);      // the numeric ID of the new input field being added

                // create the new element via clone(), and manipulate it's ID using newNum value
                var newElem = $('#condition' + num).clone().attr('id', 'condition' + newNum);
                // create the new element via clone(), and manipulate it's ID using newNum value
                var newElem1 = $('#logical' + num1).clone().attr('id', 'logical' + newNum1);

                // manipulate the name/id values of the input inside the new element
                newElem.children(':first').attr('id', 'name' + newNum).attr('name', 'name' + newNum);
                // manipulate the name/id values of the input inside the new element
                newElem.children(':first').attr('id', 'name' + newNum1).attr('name', 'name' + newNum1);

                // insert the new element after the last "duplicatable" input field
                $('#condition' + num).after(newElem);
                // insert the new element after the last "duplicatable" input field
                $('#logical' + num1).after(newElem1);

                // enable the "remove" button
                $('#btnDele').attr('disabled','');

                // business rule: you can only add 5 names
                if (newNum == 5)
                    $('#btnAdd').attr('disabled','disabled');
                    // business rule: you can only add 5 names
                if (newNum1 == 5)
                    $('#btnAdd').attr('disabled','disabled');
            });

            $('#btnDele').click(function() {
                var num = $('.clonedInput').length; // how many "duplicatable" input fields we currently have
                var num1 = $('.clonedInput1').length; // how many "duplicatable" input fields we currently have
                $('#condition' + num).remove();     // remove the last element
                $('#logical' + num).remove();     // remove the last element

                // enable the "add" button
                $('#btnAdd').attr('disabled','');

                // if only one element remains, disable the "remove" button
                if (num-1 == 1)
                    $('#btnDele').attr('disabled','disabled');
                if (num1-1 == 1)
                    $('#btnDele').attr('disabled','disabled');
            });

            $('#btnDele').attr('disabled','disabled');

​`
  • 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-06T07:03:25+00:00Added an answer on June 6, 2026 at 7:03 am

    The newElem1 is inserted after the logicalxxx item, which is before the last inserted condition item. If you replace

               // insert the new element after the last "duplicatable" input field
                $('#condition' + num).after(newElem);
                // insert the new element after the last "duplicatable" input field
                $('#logical' + num1).after(newElem1);
    

    with

      $('#condition' + num).after(newElem).after(newElem1);
    

    newelem1 is inserted directly after newelem and I think that gives the desired effect (updated fiddle: http://jsfiddle.net/ATzBA/2/ )

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

Sidebar

Related Questions

Attempting a beginner's tutorial. I have the following in my head: <script type=text/javascript charset=utf-8
I am attempting to save an array of longs as a text file using
I'm attempting to use JQuery to create a dynamic page header using an image
I am attempting a very simple form designed to take user input into a
I am attempting to use the .clone() in jquery. I have a $.post retuning
I am attempting to reorder a dynamically created list of CKEditors using the jQuery
Alright here is what I am attempting to do. I have a form written
I am attempting to compare a bitwise clone object to its parent to check
I'm attempting to write a TinyURL like clone in ASP.NET MVC as a first
I'm attempting to duplicate a div and append it on top of it's clone.

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.