11

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.

3

2 Answers 2

22

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.

Sign up to request clarification or add additional context in comments.

Comments

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.