I have html structured like so.
<div id='container'> <div class='item'> ... </div> <div class='item'> ... </div> <div class='item'> ... </div> ... <div id='item_final'><input type="button" id='addOne'>...</div> </div>
and what I am trying to do it add another item to the container class before the item_final div. The items are dynamic, so the number of them is unkown.
To insert element as a last child using jQuery, use the append() method. The append( content ) method appends content to the inside of every matched element.
prepend() method inserts the specified content as the first child of each element in the jQuery collection (To insert it as the last child, use . append() ).
append() & . prepend() .append() puts data inside an element at the last index; while. . prepend() puts the prepending element at the first index.
$('#container div:last').before( $('<div>') );
Try the .insertBefore method.
$('<div>').insertBefore('#item_final');
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With