[thelist] [javascript] [yui] setAttribute() failing

Hassan Schroeder hassan.schroeder at gmail.com
Thu Apr 30 18:43:40 CDT 2009


On Wed, Apr 29, 2009 at 6:41 PM, Paul Bennett <Paul.Bennett at mch.govt.nz> wrote:

> It works on one page, and then fails on another page with almost
> identical markup.

>            newRef = document.getElementById('ref-'+counter);
>
>            // get child
>
>            imgEl = newRef.firstChild;

My bet is that 'firstChild' is not always what you think it is :-)

>            if(imgEl){

the statement above would be true even if, say
  alert(imgEl.nodeName);   //   might equal "#text" rather than "IMG"

And of course you can't assign an ID to a text node.

So, for example --

var targetNode = newRef.firstChild;
while ( targetNode.nodeName != "IMG" )
{
  targetNode = targetNode.nextSibling;
  if ( ! targetNode ) break; // don't melt CPU if no IMG tag found
}
... etc.

FWIW!
-- 
Hassan Schroeder ------------------------ hassan.schroeder at gmail.com



More information about the thelist mailing list