Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Append partial html code jquery

I am building an html page and I need to add html code from one div to another dinamically.

This is my first div:

<div id="placeHolder" style="display:none">
    <div id="1">....</div>
    <div id="2">....</div>  
    <div id="3">....</div>      
</div>

And I want to add the divs with the id 1, 2 and 3 to another div(#mainDiv) but not all at once.

Is there a way to add partial html code with jquery ? I know theres a function called append but what I dont know if it is suitable for this type of ocasion..

Thanks

like image 518
Akash Avatar asked Mar 12 '26 07:03

Akash


2 Answers

I found an answer to this question by searching stackoverflow. I guess you are looking for the appendTo function like explained here. So, yes, you are right, appendTo is a good solution I think :-)

like image 181
Dustin Klein Avatar answered Mar 13 '26 21:03

Dustin Klein


Say for instance you want to move them from the second div to the first:

<div id="newPlace">
</div>

<div id="placeHolder">
  <div id="1">....</div>
  <div id="2">....</div>
  <div id="3">....</div>
</div>

Then you can select elements with jQuery, copy them to the other place with append, and remove them from there original place:

$("#placeHolder div").appendTo("#newPlace").remove();
like image 32
Noctua Avatar answered Mar 13 '26 20:03

Noctua