EnzoVici
EnzoVici

Reputation: 83

Jquery clone a div with inputs fields(and relative values)

I have to copy a div in which there are multiple input fields.

    var a = $('#divscossalina1').html();
    $('#riepilogo').html(a);

If i clone an input field directly,the relative value is cloned as well. This is not happening if i clone the container div.(the fields are cloned but not the values) Is there a way to clone all input fields with the values,simply cloning the container div?(or rather writing only one clone() function and not how many the fields are.)

Upvotes: 1

Views: 3048

Answers (2)

Greenhorn
Greenhorn

Reputation: 1700

Html:

<div id="one">
<input type="text" name="product" value="5" class="in" />
    <input type="text" name="product" value="6" class="in" />
        <input type="text" name="product" value="7" class="in" />    
 </div>
 <button id="button">Add field</button>

JQuery:

$('#button').click(function(){
    $('#one').clone().insertAfter("#one");
});

This even clones the value in them, working Fiddle

Upvotes: 3

Bigxiang
Bigxiang

Reputation: 6692

In jQuery, there is a method named "clone". you can read api: http://api.jquery.com/clone/

$('riepilogo').html($('#divscossalina1').clone());

and see my fiddle demo : http://jsfiddle.net/bigxiang/533zU/

Hope it works:)

Upvotes: 0

Related Questions