Reputation: 5283
I want to append a div inside a div which have many divs inside itself. My code is shown below:
<div id="main">
<div class="random no"></div>
<div class="random no"></div>
<div class="random no"></div>
<div class="random no"></div>
<div class="mydiv"></div>
</div>
My jQuery code is:
$("#main").append("<div class='random no'> </div>");
But it appends after the last child of div "main". How to insert that div before #mydiv
?
Upvotes: 4
Views: 1887
Reputation: 87073
$("<div class='random no'> </div>").insertBefore("#main .mydiv");
or
$("#main .mydiv").before("<div class='random no'> </div>");
or
$('#main').append("<div class='random no'> Random no</div>").after($(".mydiv"));
Related refs:
Upvotes: 9