简体   繁体   中英

JQuery append to parent div

For the following html:

<div class="section grouping">
    <div class="sectionControl">
        <div class="parent row errorOn">
            <div class="validGroupControl">
                <div class="row2 itemWrap clearfix">
                    <label>Login1 (adress e-mail)<span class="iconReq">&nbsp;</span>:</label>
                    <input type="text" class="text">
                </div>
                <div class="itemWrap clearfix">
                    <label>Input field1<span class="iconReq">&nbsp;</span>:</label>
                    <input type="password" class="text">
                </div>
                <a href="#" class="iconClose" onclick="$(this).closest('div.parent').remove();" title="remove">remove</a>
            </div>
        </div>
    </div>
    <div class="row addControl">
        <a href="#" class="button" onclick="$('div.sectionControl').append($('div.sectionControl div.parent:last').html());">Add</a>
    </div>
</div>

I want it to do the following: clicking "add" button would take <div class="parent..."> and append it to <div class="sectionControl">

Pretty much i want it so that when i click Add button new <div class="parent" will be added underneath previous <div class="parent..."> and right above:

                     </div>
                     <div class="row addControl">

when i try it with the parent() like so:

alert($('div.sectionControl div.parent:last').parent().html())

i get duplicates. so instead of adding just one i get original + what i just added. I'm not sure how to handle this.

thank you

Firstly, take that inline onclick code out and attach using jQuery:

$('div.addControl a.button').click(function () {
    var parent = $(this).closest('.section.grouping').find('.parent:last');
    parent.after(parent.clone());
});

Note the '.parent:last' , which will select only one of the divs.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM