Search code examples
javascriptjqueryclone

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.)


Solution

  • 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