10

How do I add a custom DIV to an already existing DIV with JQuery?

<div id="old-block"><div id="new-block">Lorem Ipsum</div></div>

Thanks in advance.

DezoLord
  • 123
  • 1
  • 1
  • 4

2 Answers2

20

There are many ways to insert a div inside another div.

We can use .append() to do it like this:

$('#old-block').append('<div id="new-block-2"></div>');

We can also use .appendTo() like this:

$('<div id="new-block-2"></div>').appendTo('#old-block');

Maybe we feel not so fancy today and want to use plain JavaScript:

document.getElementById('old-block').appendChild('div');

document.getElementById('old-block').innerHTML += '<div id="new-block-2"></div>';

You could also try parsing the HTML with RegEx.

Community
  • 1
  • 1
Timo
  • 595
  • 9
  • 18
0
$("#old-block").append("<div id='dynamic'></div>");
Sagar
  • 259
  • 2
  • 3
  • 14