views:

259

answers:

2

Basically what I have is this:

<body>
 <div class="class1">
 </div>
 <div class="class2">
 </div>
 <div class="class3">
 </div>
...
</body>

I have no idea why the site creator used classes instead of IDs (they're unique), but it doesn't really matter as I'm writing a GM script and so getElementsByClassName('')[0] effectively does the same thing.

How can I insert an element between the first and second div?

+1  A: 

Create your own insertAfter function

Russ Cam
thanks, that's what I was looking for!
Mala
no problem :) Take a look at my answer for some tips on writing your own cross-browser DOM traversal functions - http://stackoverflow.com/questions/920478/javascript-traversing-the-html-dom-using-childnodes-causes-errors-in-non-ie-bro/920501#920501
Russ Cam
I ended up using insertBefore instead... seemed easier.Thanks for the link!
Mala
A: 

you can use JQuery it very simple

$(".class1").after( document.createTextNode("Hello") );
openidsujoy
Using jQuery in a greasemonkey script is somewhat over the top, don't you think? ;)
Mala
No I did not think so, I just suggest you a option to your problem. Out of curiosity:It definitely does not deserves a -1 score.
openidsujoy