Hi;

I've sussed out a solution with cloning rows that are are part of a
form submit and I'd appreciate any feedback you might have.

Here's an image to follow along with:
http://www.hinde.net/images/jqueryhelpgraph.png

I have an application where users enroll in a class. After a search is
done, a list of matching classes is presented. If they click a
checkbox, a list of family members is presented, via a  select, and
the user can chose the family member to enroll in the class. It's not
unusual to enroll two kids in the same sort of class, but different
sessions for different age groups...

But what if two family members want to take the same class? Up to now,
the user would have to finish enrolling the first person and then
start over to add the second.

jQuery clone(true) to the rescue.

I've added a fam fam "add" button and some jQuery clone magic and now
the user can duplicate a class and enroll as 2nd person in the same
class with a single form submit.

But that's a problem on with the form submit. I've got two field sets
(the checkbox to indicate the class and the select to indicate the
user) with the same id & name.

This is what I've come up with:

                function addRow(me){//'me' is the button to add a row.
                        counter++;
                        var myId = $(me).attr("id");
                        var myDashLoc = myId.indexOf("_")+1;//This should 
always be '3'
                        
                        var cbid = myId.slice(myDashLoc);
                        var newri = "ri"+counter+"_"+cbid+"clone";
                        
                        
$("#ri_"+cbid).clone(true).insertBefore($("#ri_"+cbid)).attr("id",newri);//clone
the row, insert the clone before the base row and set the clone's id
to 'newri'
                        
                        $("#ri_"+cbid+" > td >
input:checkbox").attr("id","ssclone"+counter+"_"+cbid);//rename id for
the checkbox
                        $("#ri_"+cbid+" > td >
input:checkbox").attr("name","ssclone"+counter+"_"+cbid);//rename the
name for the checkbox
                        
                        $("#ri_"+cbid+" td:nth-child(2) span
select").attr("id","enclone"+counter+"_"+cbid);//rename id for the
select
                        $("#ri_"+cbid+" td:nth-child(2) span
select").attr("name","enclone"+counter+"_"+cbid);//rename name for the
select
                        
                        /*
                        This leaves the clone with the original's name. The 
buttons all
keep the same id, but they all point to the same clone, so it appears
to be working.
                        
                        */
                        
                                };

Does this seem like a reasonable approach? I had a heck of a time with
the selectors, was there an easier way?

Thanks.

 - Lee

Reply via email to