I'm trying to dynamically insert a few nodes into a webpage using insertBefore. Here's the HTML in question. (Yes, I know the HTML is terrible, but I didn't write it and can't edit it, which is why I have to insert things through the DOM instead. Long story.)
[HTML]<div id='paginaMinion' class='mod_profile_display_bottom mod_profile_display module'>
<div class="container">
<div class="title_container">
<h3 class="title">
<span class="title_span">
<a href="http://mipagina.univision.com/janiiita" class="title_span_link">
<div class="title_span_div"></div>
</a>
</span>
<div class="spreader"></div>
</h3>
<div class="spreader"></div>
</div>
<ul>
<a name="massobremi"></a>
<div id="lower_layout">
</div>
<div class="spreader"></div>
<div id="lower_layout2">
</div>
</ul>
<div class="spreader"></div>
</div>
</div>[/HTML]
Now, here's the JavaScript I've inserted after the HTML appears in the document:
Expand|Select|Wrap|Line Numbers
- var msm = document.getElementById('paginaMinion');
- var msmULArr = msm.getElementsByTagName('ul');
- var msmUL = msmULArr[0];
- var coverDiv = document.createElement('div');
- var coverAnchor = document.createElement('a');
- coverAnchor.setAttribute('href','http://www.example.com/');
- var coverAnchorText = document.createTextNode('Example 1');
- coverAnchor.appendChild(coverAnchorText);
- var coverText = document.createTextNode('Example 2');
- coverDiv.id = 'coverdiv';
- var commonBR = document.createElement('br');
- coverDiv.appendChild(coverText);
- coverDiv.appendChild(commonBR);
- coverDiv.appendChild(coverAnchor);
- msm.insertBefore(coverDiv, msmUL);