PDA

View Full Version : Tree element back to Tree node



Ramsay
29 May 2007, 8:27 AM
Hello,

I'm trying to get all nodes in a tree with a particular class name. When I have done this, I get a collection of Elements, rather than nodes. Is there any way to connect these elements back to their original Nodes in the tree?

jsakalos
29 May 2007, 10:17 AM
Hello,

I'm trying to get all nodes in a tree with a particular class name. When I have done this, I get a collection of Elements, rather than nodes. Is there any way to connect these elements back to their original Nodes in the tree?

Maybe yes, but wouldn't it be better to add a search data directly into nodes when creating and search these? I don't know it this is possible in your app, just a hint.

BernardChhun
29 May 2007, 12:09 PM
like Jozef said with a little bonus :


parentNodeInWhichYouAreLookingFor.findChild("cls", "myClass");

however, findChild() returns only 1 element. you could hack it like so to return multiple children:


findChildren : function(attribute, value){
var cs = this.childNodes;
var foo = [];
for(var i = 0, len = cs.length; i < len; i++) {
if(cs[i].attributes[attribute] == value){
// appending the node into the array
foo.append(cs[i]);
}
}
// returning the array if its length is greater than 0!
if (foo.length > 0){return foo;}
return null;

}