DezoLord DezoLord - 1 year ago 104
jQuery Question

How to insert a DIV into another DIV with JQuery?

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.

Answer Source

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').innerHTML += '<div id="new-block-2"></div>';

You could also try parsing the HTML with RegEx.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download