I want to be able to append divs to my page such that they are appended after a div of a certain class and before teh divs that follow it i.e:
<div class="header">Header DIV</div>
<!-- Want to add using javascript some HTML right here-->
<div class="one-basic-div">...</div>
<div class="one-basic-div">...</div>
<div class="one-basic-div">...</div>
<div class="one-basic-div">...</div>
It is basically raw html I wish to add. How can I do it?
Using the innerHTML attribute: To append using the innerHTML attribute, first select the element (div) where you want to append the code. Then, add the code enclosed as strings using the += operator on innerHTML.
after() The Element. after() method inserts a set of Node or string objects in the children list of the Element 's parent, just after the Element . String objects are inserted as equivalent Text nodes.
The insertAfter() is an inbuilt method in jQuery which is used to insert some HTML content after a specified element. The HTML content will be inserted after each occurrence of the specified element. Here the “content” is the HTML content which is to be inserted after the specified target.
Use the Element.insert
method:
document.observe("dom:loaded", function() {
$$(".header").first().insert({ after: "<p>Some html</p>" });
});
<script src="https://ajax.googleapis.com/ajax/libs/prototype/1/prototype.js"></script>
<div class="header">Header div</div>
<div class="one-basic-div">Other div</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