Append Before Last Child


Answer :

You could use .before() to add a sibling before the element:

$("#wrapper .content:last").before('<div class="content"><div class="subcontent">Third</div></div>'); 

.insertBefore() does the same thing with a different syntax, namely that you select the element to be added, and pass the element you want to add it before.

$("#wrapper .content:last").before('<div class="content"><div class="subcontent">Third</div></div>');
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script> <div id="wrapper">     <div class="content">         <div class="subcontent">             First         </div>     </div>     <div class="content">         <div class="subcontent">             Second         </div>     </div> </div>


You can use insertBefore():

$('<div class="content"><div class="subcontent">Some stuff</div></div>').insertBefore('#wrapper > div:last'); 

Or before():

$('#wrapper > div:last').before('<div class="content"><div class="subcontent">Some stuff</div></div>'); 

This is how I got there:

http://jsfiddle.net/lharby/00tk6avg/

var htmlString = '<div class="subcontent">Some stuff</div>';  $(htmlString).insertBefore('.content div:last'); 

Comments

Popular posts from this blog

Are Regular VACUUM ANALYZE Still Recommended Under 9.1?

Can Feynman Diagrams Be Used To Represent Any Perturbation Theory?