Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to add html after a certain div using Prototype JS?

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?

like image 592
Ali Avatar asked May 05 '10 12:05

Ali


People also ask

Can JavaScript add HTML elements?

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.

How to use after in JavaScript?

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.

How to add after div in jQuery?

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.


1 Answers

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>
like image 183
Salman A Avatar answered Sep 23 '22 00:09

Salman A