Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Clone form and increment

I have a form below that I want to clone, increment the id(att1) and it's inputs, textareas etc and append to the attendees div. Any help much appreciated. Been wrecking my head...

All I have is...

$(function () {
        var index = 1;


    $(".add").click(function () {
            index++;

        $("#att1").clone(true).removeAttr("id").attr("id", "att" + index).appendTo("#attendees");
            alert(index);
    });
});

Html:

    <div id="attendees">
            <div id="att1">
                    <fieldset>
                        <legend><span class="legend">Attendee 1 Booking Details</span></legend>
                        <p name="test">
                        <label for="A_Title_1">Title: <span class="req">*</span></label>
                        <input name="A_Title_1" id="A_Title_1" value="" type="text" class="f_input" />
                        </p>

                        <p>
                        <label for="A_Forename_1">Forename: <span class="req">*</span></label>
                        <input name="A_Forename_1" id="A_Forename_1" type="text" class="f_input" />
                        </p>

                        <p>
                        <label for="A_Surname_1">Surname: <span class="req">*</span></label>
                        <input name="A_Surname_1" id="A_Surname_1" type="text" class="f_input" />
                        </p>

                        <p>
                        <label for="A_Info_1">Additional Info: </label>
                        <textarea name="A_Info_1" id="A_Info_1" cols="20" rows="10"></textarea>
                        <span class="info">Please include any infomation relating to dietary/ access/special requirements you might have.</span>
                        </p>
                    </fieldset>
                    </div>
<a href="#" class="add">Add more</a>

    </div>

2 Answers

See the fiddle.

Add a class attendee to the div id="att1"

 <div id="att1" class="attendee">

And the JS:

$(function(){
    var template = $('#attendees .attendee:first').clone(),
        attendeesCount = 1;

    var addAttendee = function(){
        attendeesCount++;
        var attendee = template.clone().find(':input').each(function(){
            var newId = this.id.substring(0, this.id.length-1) + attendeesCount;
            $(this).prev().attr('for', newId); // update label for (assume prev sib is label)
            this.name = this.id = newId; // update id and name (assume the same)
        }).end() // back to .attendee
        .attr('id', 'att' + attendeesCount) // update attendee id
        .prependTo('#attendees'); // add to container
    };

    $('.add').click(addAttendee); // attach event
});
like image 189
Josiah Ruddell Avatar answered Jun 14 '26 23:06

Josiah Ruddell


Try this

    empty_html = $('#att1').html();

    $('.add').click(function() {

        last_id = $('#attendees div:last').attr('id').replace('attr','');

        next_id = last_id++;

        new_div = $('<div id="' + next_id + '">' + empty_html +'<div>');

        $('#attendees').append(new_div);

    });

Requires a few tweaks to your HTML, I would move the add link out of the attendees div.

like image 45
Alan Whitelaw Avatar answered Jun 15 '26 00:06

Alan Whitelaw



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!