[thelist] javascript DOM: generating and killing content

Dunstan Orchard dunstan at 1976design.com
Thu Oct 30 06:59:39 CST 2003


Hi there,

I'm having a few javascript DOM problems.

On my web page the user can choose one of four style options. If options 1 or 2
are chosen I need to generate and append a link onto the bottom of a div (it's a
different div in either case).

If options 3 or 4 are selected I need to ensure that this generated link is
killed (if it ever existed).

This is a simplified section from my script:

if (style == '1')
 {
 /* generate the div and link and append them to <div id="left-just"> */
 newlink = document.createElement('a');
 newlink.setAttribute('href', '#container');
 newlink.setAttribute('title', 'Go to the top of the next column');
 newlink.appendChild(document.createTextNode('\u2191 up to next column'));
 newdiv = document.createElement('div');
 newdiv.className = 'linkup';
 newdiv.appendChild(newlink);
 document.getElementById('left-just').appendChild(newdiv);
 }

else if (style == '2')
 {
 /* generate the div and link and append them to <div id="left-left"> */
 newlink = document.createElement('a');
 newlink.setAttribute('href', '#container');
 newlink.setAttribute('title', 'Go to the top of the next column');
 newlink.appendChild(document.createTextNode('\u2191 up to next column'));
 newdiv = document.createElement('div');
 newdiv.className = 'linkup';
 newdiv.appendChild(newlink);
 document.getElementById('left-left').appendChild(newdiv);
 }

else if (style == '3')
 {
 /* if the generated link exists */
 if (document.getElementById('linkup'))
  {
  /* remove it */
  var node = document.getElementById('linkup');
  node.parentNode.removeChild(node);
  }
 }

else if (style == '4')
 {
 /* if the generated link exists */
 if (document.getElementById('linkup'))
  {
  /* remove it */
  var node = document.getElementById('linkup');
  node.parentNode.removeChild(node);
  }
 }

Unfortunately the code for styles 3 and 4 isn't working. It doesn't seem to
recognise that this generated <div id="linkup"> exists - my 'if' statement
always fails.

And if I remove the 'if' statment and just leave:

  var node = document.getElementById('linkup');
  node.parentNode.removeChild(node);

If fails again with the error:

Error: this._eventNode has no properties

Can anyone suggest how I can check for its existance and then kill it?

Many thanks - Dunstan

---------------------------
Dorset, England
http://www.1976design.com/
http://www.1976design.com/blog/


More information about the thelist mailing list