[thelist] strange dom problem

david currey david_currey at tiscali.co.uk
Thu Feb 26 16:21:18 CST 2004


hi,

I am having trouble counting the number of divs in an element with the
result from elm.getElementsByTagName('div').length returning double
the value of elm.childNodes.length...

simplified example below....

<script type="text/javascript">
var d=document;
function makeRelative(fixed,elm2read) {

var outerreldiv=document.createElement('div');

innerrel=document.createElement('div');
innermove=document.createElement('div');
innermove.id="stupidthing"
var rt =document.getElementById(elm2read);

var eachones=rt.getElementsByTagName('li');
for (var i=0;i<eachones.length;i++) {
var contentdiv=document.createElement('div');
contentdiv.innerHTML=eachones[i].innerHTML;
var artdiv=document.createElement('div');
contentdiv.appendChild(artdiv)
contentdiv.onclick=function() {alert(this.innerHTML)}
innermove.appendChild(contentdiv);
}
innerrel.appendChild(innermove);
outerreldiv.appendChild(innerrel);
document.getElementsByTagName('body')[0].appendChild(outerreldiv)

var elmtocheck=d.getElementById('stupidthing')
var howmany=elmtochck.getElementsByTagName('div').length;
d.getElementById('readout').innerHTML="My code thinks there are " +
howmany + " divs inside stupid thing, and mysteriously only " +
elmtocheck.childNodes.length + " childNodes. Please help me before my
little brain explodes."
}
window.onload=function() {makeRelative(1,'thisUl')}
</script>
</head>
<body>
<p id="readout">

</p>
<ul id="thisUl">
<li>
one potato
</li>
<li>
two potato
</li>
<li>
three potato
</li>
</ul>

</body>



More information about the thelist mailing list