I have a parent div like this.
<div id='parent'>
<div id='child_1'>........</div>
<div id='child_2'>........</div>
<div id='child_3'>........</div>
<div id='child_4'>........</div>
</div>
I want to add one div like this:
<div id='page_number'> You are watching 5th object out of 100 </div>
Inside parent div I'm using append to do like that.
$("#parent").append("<div id='page_number'> You are watching 5th object out of 100 </div>");
But its comming after child_4 th div. But I need to display it just below of child_1. It should come like this
<div id='parent'>
<div id='page_number'> You are watching 5th object out of 100 </div>
<div id='child_1'>........</div>
<div id='child_2'>........</div>
<div id='child_3'>........</div>
<div id='child_4'>........</div>
</div>
How can I do like this.
Method 2: Using appendTo() Method: The appendTo() method in jQuery is used to insert HTML element at the end of the selected element. Example 1: This example uses appendTo() method to create div element at the end of selected element.
jQuery append() MethodThe append() method inserts specified content at the end of the selected elements. Tip: To insert content at the beginning of the selected elements, use the prepend() method.
New elements can be dynamically created in JavaScript with the help of createElement() method. The attributes of the created element can be set using the setAttribute() method.
$(document). ready(function() { $('body'). on('click', '. AddEl', function() { var $elems = $('.
make use of jquery function .prepend()
: Insert content, specified by the parameter, to the beginning of each element in the set of matched elements
$('Selector ').prepend($('<div> new div </div>'));
use prepend instead of append:
$("#parent").prepend("<div id='page_number'> You are watching 5th object out of 100 </div>");
Use prepend() instead of append():
$("#parent").prepend(
"<div id='page_number'> You are watching 5th object out of 100 </div>");
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