I have a html structure like this/
<li id="1"></li>
<li id="2"></li>
<li id="3"></li>
<li id="comment-box"></li>
now i wanna prepend
<li id="4"></li>
before comment-box.
i am submitting a form from the comment box and once its a success i wanna do the prepend.
The insertBefore() method inserts HTML elements before the selected elements. Tip: To insert HTML elements after the selected elements, use the insertAfter() method.
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() ).
prepend() The Element. prepend() method inserts a set of Node objects or string objects before the first child of the Element .
insertBefore( target ) A selector, element, array of elements, HTML string, or jQuery object; the matched set of elements will be inserted before the element(s) specified by this parameter.
Use before():
$('#comment-box').before('<li id="4"></li>')
Wouldn't this make more sense?:
$('ul#comments_container').prepend('<li id="4"></li>');
or
$('<li id="4"></li>').hide().prependTo('Ul#comments_container').slideDown("slow");
This would allow you to add it to the beginning of a ul list to put it at the end of the list just use 'append' rather than 'prepend' or 'appendTo' also works.
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