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/