Click to See Complete Forum and Search --> : Loop through div's with same name


Lumbago
08-15-2003, 02:20 PM
In my page I have quite a few div's that I need to transport over to another set of matching divs with javascript. My script looks like this (please notice that the divs have the same name):<SCRIPT>
for (i=0;i<SourceDiv1.length;i++) {
Div1[i].innerHTML = sourceDiv1[i].innerHTML;
}
</SCRIPT>

<!-- Source div's -->
<DIV ID="sourceDiv1>Data1</DIV>
<DIV ID="sourceDiv1>Data2</DIV>
<DIV ID="sourceDiv1>Data3</DIV>

<!-- Target div's -->
<DIV ID="Div1></DIV>
<DIV ID="Div1></DIV>
<DIV ID="Div1></DIV>

This all works great except when I only have *one* source- and target div. Then SourceDiv1.length is undefined and I don't know how to get around it. Can anyone help me? I would prefer the script to be browserindependent...

Khalid Ali
08-15-2003, 02:27 PM
you can not have same name for multiple div elements

Lumbago
08-15-2003, 02:29 PM
Sure I can, the script I just posted works excellent... well, I haven't tried this exact script cause I simplified it on the fly for this post but the logic is not a problem.

Khalid Ali
08-15-2003, 05:12 PM
Originally posted by Lumbago
Sure I can.

I am just surprised why you needed help then??
Oh BTW just run your code
through the HTML validator at
http://validator.w3.org

and you'll find out the elegance of your code..
good luck(believe me I mean it)

Lumbago
08-15-2003, 05:48 PM
Why the attitude? I'm sure you know you're stuff but it actually works just fine in ie5+...now I do know that IE isn't really W3C compatible but still it does work. My problem comes when I have only one DIV, itr isn't an array and I can't use myDIV.length.

Do you have a suggestion on how I should go about this then? I have a dynamic webpage with an invisible IFRAME that loads all these divs every 8 seconds and the onload-event in this frame fills quite a few matching divs in the visible part of another IFRAME. Any suggestions?

Khalid Ali
08-15-2003, 06:27 PM
Accessing an element is piece of cake,
First give unique names to all of the divs.

then do this

var allDivs = getElementsByTagName("div");

now you have on object that contains a list of all the div in the document.
then you can iterate through them just as you had the loop.

var len = allDivs.length;

for(var x=0;x<len;x++){
var singleDiv = allDivs[x];
//do whatever you want to do with the singleDiv here
}

Lumbago
08-16-2003, 05:55 AM
This still doesn't apply to the real issue in my original post; if I only have *one* source and *one* target DIV. allDivs.length will still be undefined because it isn't an array...right...? I'm no javascript-guru and I haven't tried your method but I still think that the problem would be the same...I could be wrong.

However, I got it to work by setting the length to a var and checking if it's null or not. Take a look:
<SCRIPT>
var mySize = SourceDiv1.length
if (mySize != null) {
for (i=0;i<mySize;i++) {
Div1[i].innerHTML = sourceDiv1[i].innerHTML;
}
else {
Div1.innerHTML = sourceDiv1.innerHTML;
}