Geek asks and answers

    Why does a jquery clone clone a parent, but not his children?

    I have a parent div with its child layer:

    <div id="padre" class="demo">
     <ul id="sortable1" class="droptrue ui-sortable">
      <li id="app1" class="ui-state-default toolTip">
       <div id="00" class="AppPadre"></div>
    

    So, when I clone my parent div with the following line:

    var $copia = $('#padre>*').clone();
    

    and I print my cloned variable that only shows:

    <div id="padre" class="demo">
     <ul id="sortable1" class="droptrue ui-sortable">
    

    Does anyone know why it only copies level 1? Thanks in advance. Immediately after the copy, I delete all sub-items of "padre" with $ ('# padre> *'). Remove (); and it removes all subelements, but when I add it, it adds only the 1st padre subelement.

    +3
    jquery clone
    linker85 Apr 23 '12 at 17:39
    source share
    2 answers

    You did not close the tags correctly:

    <div id="padre" class="demo">
    
         <ul id="sortable1" class="droptrue ui-sortable">
            <li id="app1" class="ui-state-default toolTip"><li>
         </ul>
    
        <div id="00" class="AppPadre"></div>
    
    </div>
    

    http://jsfiddle.net/E8uJm/


    Edit: Try the following:

    $('#padre').children().clone();
    $("#clone").append($copia);
    $('#padre').remove();
    

    http://jsfiddle.net/E8uJm/2/

    +4
    undefined Apr 23 '12 at 17:46
    source share

    Try the following:

    var $copia = $('#padre>*').clone(true, true);
    
    +1
    Uchenna nwanyanwu Apr 23 '12 at 17:45
    source share

    More articles:

    • iReport - First page lines ending on second page - java
    • Saving the state of the game Android - android
    • Why does the Find () method return an array? - c #
    • Check multiple checkboxes with jquery - jquery
    • Powershell and standard TCP / IP printer port - powershell
    • Implementing a Rails Authenticated Token and Development - authentication
    • Get item page number - c #
    • Get Position Index (rownum) - c #
    • Sort access data (input time in the wrong format) - c #
    • How Nested Properties Are Observed in Ember.Array - arrays

    All Articles

    Geek-Ask | 2020