Forum Moderators: open
jshanman, thanks!
it worked alright. one thing: the removeChild isn't necessary at all.This worked:
basket6 = xajax.$('basket_6');
node8 = xajax.$('node_8');
basket6.appendChild(node_8);
Glad it works!
-JS
Do you know if there's a way to check if the node ID im moving isnt parent of the target basket ID?
This is my tree:
<ul id="plantilla">
<li id="nodo_1">Page
<ul id="basket_1">
<li id="nodo_2">Sidebar<ul id="basket_2"></ul></li>
<li id="nodo_3">Content
<ul id="basket_3">
<li id="nodo_5">Header<ul id="basket_5"></ul></li>
<li id="nodo_6">Bar<ul id="basket_6"></ul></li>
<li id="nodo_7">Titulo<ul id="basket_7"></ul></li>
<li id="nodo_8">Data<ul id="basket_8"></ul></li>
</ul>
</li>
</ul>
</li>
<li id="nodo_10">Test<ul id="basket_10"></ul></li>
<li id="nodo_4">Footer<ul id="basket_4"></ul></li>
</ul>
If I move Page(1) inside Sidebar(2) it returns an error. Is there a way to pre-check parental relationship before moving it?
I was working on this js function but im still far to accomplish what I need:
function checkChildren(id) {
var children = document.getElementById('basket_'+id).getElementsByTagName('UL');
for (var index=0; index < children.length; index++) {
alert(children[index]);
}
}
i also came up with this:
if(xajax.$('nodo_'+id_padre_nuevo).parentNode.id == xajax.$('basket_'+id).id) {
alert('is child, so it wont be moved');
} else {
alert('is NOT child, so it will be moved');
}
Of course that only works for one level. how can i implement a recursive algorithm in there?
BTW:
xajax.$ = document.getElementById
that doesnt work on multinested trees.
This time I actually tested it :)
It now recursivly checks all parents until either its found (returns false), or it gets to the document Element at which the parentNode property is a null object (which returns false).
So you put that in a function like this:
function moveElement(ele,moveTo) {
var x = moveTo;
var found = false;
while (x.parentNode &&!found) {
if (x.parentNode == ele) found = true;
x = x.parentNode;
}
if (!found) result = moveTo.appendChild(ele);
return!found;
}
<ul id="ul0">UL0:
<ul id="ul1">UL1:
</ul>
<ul id="ul2">UL2:
<li id="li1">LI1: move me to ul1</li>
</ul>
</ul>
<input type="button" value="move test 1" onclick="moveElement(document.getElementById('li1'),document.getElementById('ul1'))">
<input type="button" value="try to move parent into child" onclick="moveElement(document.getElementById('ul0'),document.getElementById('ul1'))">
I tested it in IE6 and Firefox 1.5.
- JS