S
santosh.pjb
I have a button on a page whose onclick funtion is posted below. I am
basically trying to get all the spans in the page and list them in 2
columns. I get the list of spans using getElementsByTagName('span').
I dont want to move the spans themselves into my 2 column list(i.e I
want them to stay where they are on the page), so I figured I needed
to clone each span. Each time I make a clone, the clone somehow gets
added to my original list of spans, so I end up in an infinite loop
adding the first few elements again and again. How is this supposed to
be done? Javascript n00b here...any advise is greatly
appreciated...
function getSpans()
{
var allspans=document.getElementsByTagName("span");
var infoDiv = null;
var tmp;
for(var element, i=0;element=allspans;i++)
{
tmp=element.cloneNode(true);
alert(i +' '+tmp.innerHTML +' ' +allspans.length); <<<<<
allspans keeps increasing
if( i%2 == 0 ){
if( i!=0 ) list1.appendChild(infoDiv);
infoDiv=document.createElement('div');
}
infoDiv.appendChild(tmp);
}
}
list1 is a div id
Thanks
basically trying to get all the spans in the page and list them in 2
columns. I get the list of spans using getElementsByTagName('span').
I dont want to move the spans themselves into my 2 column list(i.e I
want them to stay where they are on the page), so I figured I needed
to clone each span. Each time I make a clone, the clone somehow gets
added to my original list of spans, so I end up in an infinite loop
adding the first few elements again and again. How is this supposed to
be done? Javascript n00b here...any advise is greatly
appreciated...
function getSpans()
{
var allspans=document.getElementsByTagName("span");
var infoDiv = null;
var tmp;
for(var element, i=0;element=allspans;i++)
{
tmp=element.cloneNode(true);
alert(i +' '+tmp.innerHTML +' ' +allspans.length); <<<<<
allspans keeps increasing
if( i%2 == 0 ){
if( i!=0 ) list1.appendChild(infoDiv);
infoDiv=document.createElement('div');
}
infoDiv.appendChild(tmp);
}
}
list1 is a div id
Thanks