470,575 Members | 1,265 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 470,575 developers. It's quick & easy.

insertBefore generates "node was not found" error

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>
<div class="spreader"></div>

<div class="spreader"></div>

<a name="massobremi"></a>
<div id="lower_layout">
<div class="spreader"></div>
<div id="lower_layout2">

<div class="spreader"></div>

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);
  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
3 8187
210 100+
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
428 256MB

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
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.

Similar topics

reply views Thread by Ari Royce Hidayat | last post: by
1 post views Thread by Casper Stendal | last post: by
8 posts views Thread by webgenius | last post: by
1 post views Thread by livre | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.