appendTo and prependTo with native javascript

0

I would like to know how I execute the functions of jQuery "appendTo" and "prependTo" with pure Javascript. I attach an example:

$(document).ready(function(){
  $('.item').appendTo('.contenedor');
});
.item{
  border:1px solid #d3d3d3;
}
.contenedor{
  border:1px dotted red;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>

<div class="item">Item</div>
<div class="item">Item</div>
<div class="item">Item</div>
<div class="item">Item</div>

<div class="contenedor"></div>
    
asked by Dan784 21.11.2018 в 21:59
source

2 answers

1

You can try with this code of% pure JavaScript .

//get items from HTML
var item = document.getElementsByClassName("item");

for (var i = 0; i < item.length; i++) {
  //add to contenedor
  document.getElementById('contenedor').appendChild(item[i]);
}
.item{
  border:1px solid #d3d3d3;
}
.contenedor{
  border:1px dotted red;
}
<div class="contenedor" id="contenedor"></div>
<div class="item">Item</div>
<div class="item">Item</div>
<div class="item">Item</div>
<div class="item">Item</div>

Greetings !!!

    
answered by 22.11.2018 / 00:24
source
-2

You can do it with insertAdjacentHTML()

More information at:

link

    
answered by 21.11.2018 в 22:04