0

How can i append to a DOM element a div which is saved as a string in a variable?

as for example:

var toAppend = "<div id="divID" class="hello" contenteditable>hi hi</div>";

how can I append it using for example:

$('#parentDiv').appendChild( ... );

1 Answers1

0

As follow:

var str = '<div id="divID" class="hello" contenteditable>hi hi</div>';
$('#parentDiv').append(str);
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div id='parentDiv'/>
Ele
  • 33,468
  • 7
  • 37
  • 75
  • So with jquery .append i can append it as it is, cool thanks. –  Oct 19 '18 at 22:10
  • Does it append it below the parentDiv in this case? If i have other divs inside parentDiv already for instance, it goes as last? –  Oct 19 '18 at 22:12
  • 2
    It works just like appendChild. It adds the element(s) as the last children of the element you are appending to – Taplar Oct 19 '18 at 22:15