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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 16, 20262026-05-16T21:01:41+00:00 2026-05-16T21:01:41+00:00

I’m having trouble with some jQuery stuff, please help me out. Below is my

  • 0

I’m having trouble with some jQuery stuff, please help me out. Below is my form structure:

<form name="imageUploadForm" id="imageUploadForm" action="" method="POST" enctype="multipart/form-data" >
   <table border="0" width="80%" cellpadding="2" cellspacing="3" id="mainTable">
   <tbody>
    <tr>
            <td> 
                  <table border="0" id="imageTable" class="imageTable">
                    <tr>
                      <td><label for="image_title"><sup>*</sup>Image Title:</label></td>
                      <td><input type="text" name="image_title[]" id="image_title[]"></td>
                    </tr>
                    <tr>
                      <td><sup>*</sup>Image:</td>
                      <td><input type="file" name="main_image[]" id="main_image[]" ></td>
                    </tr>
                  </table>
            </td>
        </tr>
    </tbody>
    <tfoot>
        <tr>
            <td colspan="2"><input type="button" id="addRow" value="Add More" /></td>
        </tr>
    </tfoot>
    </table>    
</form> 

What I want is this: when I click on an Add More Button, a new table (i.e. clone of imagetable) appends to mainTable, here is my jQuery Code:

jQuery(document).ready(function() {
    var count=1;
    jQuery('#addRow').click( function () {
        var Clonedtable = jQuery("#imageTable").clone(true);
        Clonedtable.appendTo('table#mainTable');
    })
});
  • I need to create different IDs for table, and the two inputs
  • Is it valid to make an array of IDs?
  • I want to append the cloned table append the tbody’s last table, not just after the main ImageTable
  • Both inputs should be empty.

Please suggest me optimized method. Thanks.


UPDATE: i have changed the table structure ( i was wrong before ), i want to change for attribute value of label according to next input id and delete <sup>*</sup> tag. i write below code, everything is working fine, but i need to write collectively, i don’t understand how to write it collectively, please suggest me

 jQuery('#addRow').live('click', function () {
        var quantity = jQuery('table[class^=imageTable]').length;
        var clonedRow = jQuery('#mainTable > tbody > tr:first').clone(true);
        var textID = clonedRow.find(':text').attr('id');
        clonedRow.find('label').attr('for', function () {
            return textID + quantity;
        });
        clonedRow.find('th').text('Image '+(++quantity) + ' :');
        clonedRow.find('sup').remove();     
        clonedRow.attr('id', function () {
            return this.id + quantity;
        }).find(':text,:file').attr('id', function () {
            return this.id + quantity;
        }).val('').end().appendTo('#mainTable');
    });
  • 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-16T21:01:42+00:00Added an answer on May 16, 2026 at 9:01 pm

    EDIT: There are some issues with accessing properties of cloned elements. I changed the answer to use the native getAttribute and setAttribute instead.

    jQuery('#addRow').click(function() {
        // Get current count of imageTables and use that value for IDs
        var quantity = jQuery("table[id^=imageTable]").length;
    
        // clone the table
        var clone = jQuery("#imageTable").clone(true);
    
        // use native DOM methods to update the ID
        clone[0].setAttribute('id', clone[0].getAttribute('id') + quantity);
    
        // find any text or file inputs, and iterate over them
        clone.find(':text,:file').each(function() {
              // use native DOM methods to update the ID
            this.setAttribute('id', this.getAttribute('id') + quantity);
              // set the value to ""
            this.value = "";
        });
         // append to the <td>
        clone.appendTo('#mainTable > tbody:last > td');
    });​
    

    Original answer:

    Try this:

    <script type="text/javascript">
        jQuery(document).ready(function() {
            jQuery('#addRow').click( function () {
                       // Get current count of imageTables and use that value for IDs
                var quantity = jQuery("table[id^=imageTable]").length;
                       // Clone the main table
                jQuery("#imageTable").clone(true)
                       // Change its ID to the current ID plus the quantity variable
                     .attr( 'id', function() { return this.id + quantity; })
                       // find any text or file inputs
                     .find( ':text,:file' )
                       // change their IDs
                     .attr( 'id', function() { return this.id + quantity; })
                       // set the input values to ""
                     .val( "" )
                       // return to the cloned table
                     .end()
                       // append wherever you want it.
                       // As the comment below your question states,
                       //   this is not a valid placement
                     .appendTo('#mainTable > tbody:last');
            })
        });
    </script>
    

    EDIT: Fixed typo in .find() where the comma was outside the quotation marks.

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

Sidebar

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.