tags:

views:

65

answers:

3

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');
    });
A: 

if you want to keep simple change

          var table= $(".imageTable").html();
          table.appendTo("#mainTable");
JapanPro
i want new ID and new name for next table's each row.....what means of your code?? do not use `$` when using `jQuery` everywhere.. need to improve Buddy
JustLearn
@JustLearn YOU need to improve 'buddy'. Start with HTML.
jessegavin
i was just focusing on the core part , that instead of cloning , you can take html and append it straight forward. your table structure you need to look as it s not correct now.
JapanPro
@JustLearn: unless you're using another library that defines `$` (or if you're using that variable yourself), then `$` is already an alias for `jQuery`, so your code can be more concise.
Matt Ball
A: 

You're trying to append the cloned element to the wrong element, you're targeting table#mainTable, when you really want to append it to the tbody.

Change this line

Clonedtable.appendTo('table#mainTable');

To this

Clonedtable.appendTo('table#mainTable tbody');

But you really don't want to do that either. You really shouldn't have any tables directly in a tbody, but rather a td element.

jessegavin
+1  A: 

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.

patrick dw
Superb Solution.... but i need that cloned table appear after mainTable, not before main table
JustLearn
@patrick appending `Table` to a `tbody` is valid ??
Ninja Dude
@Avinash - Please read my code comment in the answer. *"As the comment below your question states, this is not a valid placement"*
patrick dw
@JustLearn - Give me a minute. I know there are some oddities with `clone()` in jQuery.
patrick dw
Got it.....You are Right, i ws wrong... forgot to write td inside tbody
JustLearn
@JustLearn - I updated my answer. With regard to the `appendTo` not appearing where you expect, it is because this is an invalid placement as some of the comments here have noted. So the browser is doing its best to figure out where it should go. You should change your HTML structure to make it valid. If you use Firebug or some other tool to view the rendered HTML, you'll see that even the original table is getting kicked out of its position where you placed it.
patrick dw
@JustLearn - One more thing. `[]` are not valid ID characters in HTML 4. You should probably change that.
patrick dw
@patrick, Thanks a lot for Help ,i have changed table structure, please tell me now how to clone of each row inside tbody
JustLearn