Kanan Farzali
Kanan Farzali

Reputation: 1055

Appending child html after specific already appended child - jquery

Trivial question, but couldn't figure it out. Googled it a bit, but surprisingly didn't find a clear answer for my particular example.

I have got an html bit:

<div id="main">
   <div id="box15"></div>
   <div id="box12"></div>
   <div id="box7"></div>
   <div id="box1"></div>
   <div id="box3"></div>
</div>

How would I append html bit <div id="box19"></div> to main id after specific child, for example after <div id="box12"></div>?

Upvotes: 0

Views: 53

Answers (3)

Satpal
Satpal

Reputation: 133403

You can use .insertAfter()

Insert every element in the set of matched elements after the target.

Code

$('<div id="box19">box19</div>').insertAfter("#box12");

DEMO

OR

You can use .after()

Insert content, specified by the parameter, after each element in the set of matched elements.

Code

$("#box12").after('<div id="box19">box19</div>' );

DEMO with after

Upvotes: 2

Rachit Patel
Rachit Patel

Reputation: 862

var div = document.getElementById('main');

div.innerHTML = div.innerHTML + '';

Upvotes: 0

Dhaval Marthak
Dhaval Marthak

Reputation: 17366

You can Use insertAfter()

$('<div id="box19"></div>').insertAfter("div#box12")

Demo

Upvotes: 1

Related Questions