Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

jquery how clone bootstrap row

Tags:

jquery

I'm trying to clone a bootstrap row but every time I get a multiple-line (1-2-4-8-etc)

$("#clone").click(function() {
    $(".cloned-row:first").clone().insertAfter(".cloned-row");
});

HTML

 <div class="cloned-row">
    <div class="row">
        <div class="col-md-4">
            <div class="form-group">
                <label class="control-label" for="foo">Foo</label>
                <input type="text" class="form-control" name="foo[]" />
            </div>
        </div>
        <div class="col-md-4">
            <div class="form-group">
                <label class="control-label" for="bar">bar</label>
                <input type="text" class="form-control" name="bar[]" />
            </div>
        </div>
    </div>
</div>


<div class="row">
    <div class="col-md-2">
        <input type="button" class="form-control btn-info" value="clone" id="clone">
    </div>
    <div class="col-md-2">
        <input type="button" class="form-control btn-danger" value="remove" id="remove">
    </div>
</div>

How could I do to clone only a div cloned-row at a time? Thank You

like image 205
Gus Avatar asked Jan 27 '15 16:01

Gus


People also ask

How to clone using jQuery?

To clone an element using jQuery, use the jQuery. clone() method. The clone() method clones matched DOM Elements and select the clones. This is useful for moving copies of the elements to another location in the DOM.

What is clone() in jQuery?

jQuery clone() Method The clone() method makes a copy of selected elements, including child nodes, text and attributes.

What is clone() in javascript?

Cloning in javascript is nothing but copying an object properties to another object so as to avoid creation of an object that already exists. There are a few ways to clone a javascript object. 1) Iterating through each property and copy them to a new object.


1 Answers

You are simply telling it to insert after every cloned row with insertAfter(".cloned-row"). That is causing additional cloning you did not expect.

Just insert after the last one using :last

e.g.

insertAfter(".cloned-row:last")

JSFiddle: http://jsfiddle.net/TrueBlueAussie/u8zdutfy/

like image 151
Gone Coding Avatar answered Sep 18 '22 00:09

Gone Coding