PDA

View Full Version : Why did you use appendChild function inside insertChild?



Zdeno
16 Jul 2012, 4:26 AM
When you check source code of function insertChild, you will find following code:



insertChild: function(index, node) {
var sibling = this.childNodes[index];
if (sibling) {
return this.insertBefore(node, sibling);
}
else {
return this.appendChild(node);
}
},


Why did you use appendChild function when sibling does not exists? Instead of appendChild you have to check count of childNodes and then you can say if you want put node to childNodes or you have to call appendChild. AppendChild will add node at the end of array but i wanted insert node to specific index. Thats why i used insertChild instead of appendChild.

tvanzoelen
16 Jul 2012, 6:48 AM
I do not understand your question. If there are no siblings or the position in the input doesn't return a sibling, then the node is added at the end.

That what is written in the function.

If siblingnode by index is found
then insert node before that sibling
Else
insert at end

Is correct I think