javascriptjqueryappendclonenodejquery-clone

Does jQuery use clone in appendTo / prependTo


Hello guys I'm writing a little version of jquery(not all of that) for my website.(for shortnize my code)

i reached to prependTo and appendTo methods which raise a question for me:

Does jQuery use clone method or cloneNode method to build this functions?


Thanks....


Solution

  • According to the appentTo() docs, it depends if you're targeting one existing element or more than one.

    We can also select an element on the page and insert it into another:

    $( "h2" ).appendTo( $( ".container" ) );
    

    If an element selected this way is inserted into a single location elsewhere in the DOM, it will be moved into the target (not cloned) and a new set consisting of the inserted element is returned:

    <div class="container">
      <div class="inner">Hello</div>
      <div class="inner">Goodbye</div>
      <h2>Greetings</h2>
    </div>
    

    If there is more than one target element, however, cloned copies of the inserted element will be created for each target except the last, and that new set (the original element plus clones) is returned.