By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
459,466 Members | 1,158 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 459,466 IT Pros & Developers. It's quick & easy.

insertBefore generates "node was not found" error

P: 14
I've been playing around with this for a while. I bet the answer is pretty obvious, but I'm just not seeing it. I'm also reasonably certain I've run into this problem before.

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
  1. var msm = document.getElementById('paginaMinion');
  2. var msmULArr = msm.getElementsByTagName('ul');
  3. var msmUL = msmULArr[0];
  4. var coverDiv = document.createElement('div');
  5. var coverAnchor = document.createElement('a');
  6. coverAnchor.setAttribute('href','http://www.example.com/');
  7. var coverAnchorText = document.createTextNode('Example 1');
  8. coverAnchor.appendChild(coverAnchorText);
  9. var coverText = document.createTextNode('Example 2');
  10. coverDiv.id = 'coverdiv';
  11. var commonBR = document.createElement('br');
  12. coverDiv.appendChild(coverText);
  13. coverDiv.appendChild(commonBR);
  14. coverDiv.appendChild(coverAnchor);
  15.  
  16. msm.insertBefore(coverDiv, msmUL);
The insertBefore causes a "node was not found" error and nothing is inserted into the document. Does anyone have any idea why?
Jun 18 '08 #1
Share this Question
Share on Google+
3 Replies


100+
P: 210
The insertBefore causes a "node was not found" error and nothing is inserted into the document. Does anyone have any idea why?
msm is not the immediate parent of msmUL. Try
Expand|Select|Wrap|Line Numbers
  1. msmUL.parentNode.insertBefore(coverDiv, msmUL)
Jun 19 '08 #2

100+
P: 428
<h3>
<span>
<a>
<div></div>
</a>
</span>
<div></div>
</h3>

What is a browser to do- an <h3> is not supposed to contain block elements,
and neither are <span> or <a> elements.

Here is a div inside an <a> inside a <span> inside an <h3>, which also has another <div> after the <a>.
I'm surprised your list isn't in there too. But how is the browser supposed to assemble a DOM view of that?
Jun 19 '08 #3

P: 14
I had no idea you had to reference the immediate parent -- I thought any parent would do. The parentNode solution was a great idea and worked like a charm -- thanks!

I have to work with some pretty badly formed HTML on a day-to-day basis, and a substantial part of my job is making it do things it can't or isn't supposed to with JavaScript. Browsers will always assemble a DOM view of whatever they see -- it just may not be anything remotely like what you'd expect!
Jun 19 '08 #4

Post your reply

Sign in to post your reply or Sign up for a free account.