473,395 Members | 1,905 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,395 software developers and data experts.

Question about cloneNode behavior

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];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
Jun 27 '08 #1
3 1485
On Apr 16, 2:32 pm, santosh....@gmail.com wrote:
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.
The collection returned by getElementsByTagName is live, that is, as
you add more spans to the document, more are added to your collection.

How is this supposed to
be done?
Convert the collection to an array, something like:

function toArray(obj) {

if (typeof obj.length != 'number') { return [obj]; }

var result = [];
for (var i=0, len=obj.length; i<len; i++) {
result.push(obj[i]);
}
return result;
}

You can also work backward through the collection using its initial
length if you are adding the spans lower in the DOM than the last in
the initial collection, but that doesn't seem to be a good idea here
(and can cause maintenance issues anyway).

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];i++)
Why not the more common:

var element;
for (var i=0, len=allspans.length; i<len; i++) {
element = allspans[i];
{
tmp=element.cloneNode(true);
alert(i +' '+tmp.innerHTML +' ' +allspans.length); <<<<<
allspans keeps increasing
if( i%2 == 0 ){
You could just use i%2
if( i!=0 ) list1.appendChild(infoDiv);
Don't expect element IDs to be global variables, that is an IE
invention. Use getElementById.

infoDiv=document.createElement('div');
}
infoDiv.appendChild(tmp);
}

}

list1 is a div id
function getSpans(id) {

var target = document.getElementById(id);
var allspans = toArray(document.getElementsByTagName('span'));
var div = document.createElement('div');

for (var i=0, len=allspans.length; i<len; i++) {
div.appendChild(allspans[i].cloneNode(true));
}

target.appendChild(div);
}

Needs a bit of feature detection and testing, but shows the concept I
hope.
--
Rob

Jun 27 '08 #2
Thanks a lot!

The toArray() was what I needed. Didn't know about live collections
and lost a bit of hair trying to figure out what it was doing.

About the 'for' loop style, it's part of some existing code and I
hadn't actually given it any thought until you pointed out the
conventional form. It works fine though. The i%2==0 is required as I
am trying to create a div every 2 spans.

Thanks once again for all the pointers...much appreciated.
Jun 27 '08 #3
RobG wrote:
function toArray(obj) {

if (typeof obj.length != 'number') { return [obj]; }

var result = [];
for (var i=0, len=obj.length; i<len; i++) {
result.push(obj[i]);
}
return result;
}
Given that Array.prototype.push() requires JScript 5.5, much more efficient
and equally compatible is

function toArray(obj)
{
return [].slice.call(obj);
}

See http://PointedEars.de/es-matrix
PointedEars
--
realism: HTML 4.01 Strict
evangelism: XHTML 1.0 Strict
madness: XHTML 1.1 as application/xhtml+xml
-- Bjoern Hoehrmann
Jun 27 '08 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
by: MonkeyBoy | last post by:
I am doing some some HTML manipulation in client-side script (IE5.x and IE6.x browsers only). Something like.. var tmpHTML = oTable.outerHTML // do something to the HTML her oTable.outerHTML =...
8
by: Richard Trahan | last post by:
I have the following line of code: var newopt = document.createElement("option"); I addChild this to a select list node and it works fine. I want to attach an object reference to newopt, but...
3
by: Marco Rizzi | last post by:
Hi all, i'm trying to add same node to XmlDocument. This is Xml that I want to create: <TableColumns> <TableColumn><Width>1.5in</Width></TableColumn>...
1
by: Petr Felzmann | last post by:
Hi, why the duration of call the CloneNode(true) is steadily increasing? First call is 0.004s and 100th is 0.16s! XML file is 5kB only, no disk swap. XmlDataDocument xml = new...
0
by: Sullivan WxPyQtKinter | last post by:
Hi, I am now using minidom for my current development. I use cloneNode method in Element object, but it just does not work. The test code is very simple as follows: =========CODE==============...
2
by: Csaba Gabor | last post by:
One of the things that I never understood was the motivation for not being able to iterate over the event listeners added via ..addEventListener It's particularly vexing because if you do...
1
by: jaktharkhan | last post by:
Hi, I really really need help in trying to figure out how can I do a CloneNode on an Iframe where the cloned IFRAME clones with all its contents?. Basically what I am doing is dynamically building...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.