473,395 Members | 1,516 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.

javascript recursive appendChild function

Hi all,

I am coding an AJAX DHTML whatever application and I was fed up with
always typing a lot of appendChild() functions.

I created a custom one called append_children() and wanted to share it
if anyone need such a function.

function append_children() {
var a = append_children.arguments;
for ( var i = a.length - 1; i > 0 ; i-- ) {
try {
a[i-1].appendChild(a[i]);
} catch(e) {
for ( var j = 0; j < a[i].length; j++ ) {
append_children(a[i-1], a[i][j]);
}
}
}
}

You can see a working example with this URL :
http://amisphere.blogspot.com/2005/0...pendchild.html

If anyone has some suggestions about modifications or other ways to
achieve this task, please post...

Sam

Aug 1 '05 #1
2 10824


sa*********@gmail.com wrote:

I am coding an AJAX DHTML whatever application and I was fed up with
always typing a lot of appendChild() functions.

I created a custom one called append_children() and wanted to share it
if anyone need such a function.

function append_children() {
var a = append_children.arguments;
You should simply use
var a = arguments;
here as functionName.arguments is deprecated.
for ( var i = a.length - 1; i > 0 ; i-- ) {
try {
a[i-1].appendChild(a[i]);
} catch(e) {
for ( var j = 0; j < a[i].length; j++ ) {
append_children(a[i-1], a[i][j]);
}
}
}
}

You can see a working example with this URL :
http://amisphere.blogspot.com/2005/0...pendchild.html


You do not explain in your post here what is supposed to be passed to
the function but in the article at that URL you do so I borrow from there:

You want to replace the code

function menu_create() {
// create HTML nodes
menu_div = document.createElement('div');
menu_span1 = document.createElement('span');
menu_span1_txt = document.createTextNode(' Buy ');
menu_span2 = document.createElement('span');
menu_span2_txt = document.createTextNode(' Sell ');
menu_span3 = document.createElement('span');
menu_span3_txt = document.createTextNode(' Share ');

// join the nodes together
menu_span1.appendChild(menu_span1_txt);
menu_span2.appendChild(menu_span2_txt);
menu_span3.appendChild(menu_span3_txt);
menu_div.appendChild(menu_span1);
menu_div.appendChild(menu_span2);
menu_div.appendChild(menu_span3);

// show the menu on the page
document.body.appendChild(menu_div);
}

menu_create();

with the code

function append_children() {
var a = append_children.arguments;
for ( var i = a.length - 1; i > 0 ; i-- ) {
try {
a[i-1].appendChild(a[i]);
} catch(e) {
for ( var j = 0; j < a[i].length; j++ ) {
append_children(a[i-1], a[i][j]);
} } }
}

function menu_create() {
// create HTML nodes
menu_div = document.createElement('div');
menu_span1 = document.createElement('span');
menu_span1_txt = document.createTextNode(' Buy ');
menu_span2 = document.createElement('span');
menu_span2_txt = document.createTextNode(' Sell ');
menu_span3 = document.createElement('span');
menu_span3_txt = document.createTextNode(' Share ');

// join the nodes together and show the menu on the page
append_children(document.body, menu_div, [[menu_span1, menu_span1_txt],
[menu_span2, menu_span2_txt],[menu_span3, menu_span3_txt]]);

}

menu_create();
That does not create the same DOM structure however, the original code
creates the text nodes as child nodes of the <span> elements e.g.

<div><span> Buy </span><span> Sell </span><span> Share </span></div>

while with your function (if it works at all) you get the <span>
elements and the text nodes as siblings e.g.

<div><span></span> Buy <span></span> Sell <span></span> Share </div>

That is what happens with Mozilla and IE, Opera 8 does not intend to
play to your rules "if the child element is an array, the appendChild
function fails" and creates only a <div> element.

So you need to recode your function to achieve the same structure as the
original code.

And you should try to avoid try/catch respectively hoping on some error
being thrown, if you know that some elements in the array passed in can
be arrays itself then you can check whether the element has a length
property and then process it recursively as you want.
--

Martin Honnen
http://JavaScript.FAQTs.com/
Aug 1 '05 #2
Hi Martin,
You should simply use
var a = arguments;
here as functionName.arguments is deprecated.
Thanks for the tip, i just used my Javascript Bible 5th edition where
it still shows the functionName.arguments as an example.
You do not explain in your post here what is supposed to be passed to
the function but in the article at that URL you do so I borrow from there:

You want to replace the code

function menu_create() {
// create HTML nodes
menu_div = document.createElement('div'); .... }

menu_create();
That does not create the same DOM structure however, the original code
creates the text nodes as child nodes of the <span> elements e.g.

<div><span> Buy </span><span> Sell </span><span> Share </span></div>

while with your function (if it works at all) you get the <span>
elements and the text nodes as siblings e.g.

<div><span></span> Buy <span></span> Sell <span></span> Share </div>

That is what happens with Mozilla and IE, Opera 8 does not intend to
play to your rules "if the child element is an array, the appendChild
function fails" and creates only a <div> element.

Oops ! :) I feel so stupid now...

I checked the example with the DOM Inspector, looks like i was too
tired to see my mistakes.
So you need to recode your function to achieve the same structure as the
original code.

Sir, yes, sir.
And you should try to avoid try/catch respectively hoping on some error
being thrown, if you know that some elements in the array passed in can
be arrays itself then you can check whether the element has a length
property and then process it recursively as you want.


I used the try/catch method to detect whether an element is an array or
not because there is no ( typeof x == array ) and a TextNode has a
length property.

You are right, this is not elegant at all. With a night of sleep, a
better method is :

if ( element.nodeType ) alert( element + ' is a node.');
else alert( element + ' should be an array.');

Well, i have recoded the function with my brain plugged. I'm not very
satisfied but the final version works fine with Mozilla, IE and Safari.

I wanted to use the arguments.callee.caller but it doesn't work with
Safari, so i have done it this way :

I want to append a document structure at once, modeled with arrays.

To obtain this structure :
<div><span></span><span></span></div><span></span>

I feed the append_children function this way :
append_children([[document.body, div, [[span1, span1_txt], [span2,
span2_txt]]], [document.body, [span3, span3_txt]]]);

The solution works this way :
1. I use a recursive function to remove the arrays
2. While removing those arrays, i record the array structures inside a
global variable
3. I use this structure variable to append the children nodes to the
document

Working example :
http://test.amisphere.com/code/append_children.html

Can you make a few comments on what i could improve in the code ?

--
Sam
http://amisphere.blogspot.com/
--

Aug 3 '05 #3

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

Similar topics

4
by: JesusFreak | last post by:
From: us_traveller@yahoo.com (JesusFreak) Newsgroups: microsoft.public.scripting.jscript Subject: toolbar script problem NNTP-Posting-Host: 192.92.126.136 Recently, I downloaded the following...
4
by: Sergio del Amo | last post by:
i, I have the next html page <html> <head> <script> <!-- function insertcode() { var code ="<p> blablabal babala babababab</p><h1>here comes header</h1><span>fadfafa<a...
4
by: bboyle18 | last post by:
Hi, I am working with a table sorting script which can be found here http://www.workingwith.me.uk/articles/scripting/standardista_table_sorting This script works very nicely, but when there is a...
7
by: julian.tklim | last post by:
Hi, I need to build an editable Datagrid with add & delete buttons on each row using javascript. DataGrid need not be pre-populated with values. To make the thing complicated, one of the...
3
by: SM | last post by:
Hello, Im trying to access elements in my XML file using the JavaScript DOM but i'm not sure how. I use AJAX to access the XML and then use the responseXML property to access the XML file data. I...
0
by: Kaushik C P | last post by:
Can you tell me , why this code does not run: <%@ Page Language="C#" MasterPageFile="~/MasterPage.master" AutoEventWireup="true" CodeFile="AJAX6.aspx.cs" Inherits="Experiments_AJAX_AJAX6"...
6
by: ApOG | last post by:
Hello everyone, I have this javascript code working perfectly with IE, but with firefox nothing happens when running the function... function add_div_field () { var ni =...
6
by: ismomo | last post by:
Hi all, I am facing a problem in recursive. I would like to create node strcuture similar with the xml structure shown below. However, I am not able to create the node with the sub I written in perl....
3
chathura86
by: chathura86 | last post by:
hi i wrote the following code to populate a table dynamically it works perfectly on Firefox but not in IE 7 does not show any errors on IE7 either please help me
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:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
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...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
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
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
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...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

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.