Emre Aydin
Emre Aydin

Reputation: 553

how to add element right after another element?

I have a html structure like below:

<div class="element1>

</div>

I want to create another div.element right after div.element1 so I use element2.appendTo("div.element1") but it's being created like below:

<div class="element1>
 <div class="element2">

 </div>
</div>

What I really what to be done is like below:

<div class="element1>

</div>
<div class="element2">

</div>

How this can be done?

Upvotes: 1

Views: 99

Answers (3)

Mario Araque
Mario Araque

Reputation: 4572

You should use after to insert elements as you want.

Try it:

$('.element1').after(element2);

Upvotes: 2

George G
George G

Reputation: 11

With jQuery you can user after or insert to the parent:

$('#element1').after('#element2')    
$('#element1').parent().append('#element2')

Upvotes: -1

collab-with-tushar-raj
collab-with-tushar-raj

Reputation: 1287

use insertAfter() :-

$(".element2").insertAfter($(".element1"));

Upvotes: 0

Related Questions