The problem is with the selector: "A clone of the original html piece of code works OK", but, A clone from cloned piece of code marks the selector as "undefined", in other words, the second time that I clone the table the selector doesnt work anymore for those cloned items.

Why aren't you just cloning the original all the time with :first instead of :last in the selector?
–
googletorpOct 13 '09 at 15:45

The :last part of it is important because the selector is also used to determine what the new form will be inserted after. but I've testes like that with no results, the same behaviour with all browsers now :(
–
panchicoreOct 13 '09 at 16:15

Your code works well, as posted, on IE6, 8, 8 on 7 compatibility mode, and Firefox: jsbin.com/omego . Of course, some controls are missing, but the code works.
–
KobiOct 19 '09 at 20:01

thx for test, cloning works OK, but inside, the selectors are not. If you can, do it in your server with your lang of preference, get a POST o GET from that form and plz tellme what you see =)
–
panchicoreOct 20 '09 at 3:44

4 Answers
4

This is a known jQuery bug, though they claim it is fixed.
One option here is to use .html(), and clone them manually. This will not clone events and saved .data, which may be an issue for you. .live can help if you have events here.

If the only thing you need is to change the names and id, a better option is to use a regular expression (this clones the events from the first element, mind you):

var name = $(this).attr('name').replace(/-\d+-/,'-' + total + '-');

this will search for -number-, and replace it, so it finds the last number on all browsers, or -0- on IE.

As a side note - your code is a little messy. jQuery code should be inside $(document).ready (the click), you have a table with no body (no <tr>,<td> - the inputs are thrown out), and the code has some duplications.
Although it didn't help in this case, invalid DOM and not using the ready event can cause problems.

Hi Kobi, first than all: thanks 4 understandme, im taking all your advices (notes) and fixed messy code, and applying jsbin.com/evapu to my code it results this error: $(selector).after(newElement); Node cannot be inserted at the specified point in the hierarchy" code: "3, the element is cloned but when JS run this line, return this error and delete the cloned items.
–
panchicoreOct 23 '09 at 14:49

OMG, if I remove the $(selector).after(newElement); line, it now works fine in IE, why Kobi?
–
panchicoreOct 23 '09 at 14:56