PDA

View Full Version : How do you apply an attribute to all childNodes?


hotwheelharry
06-10-2008, 11:23 AM
I wanted to know how to make a script that would take a xml element (ie. <div>) and then apply an attribute to it, it's childNodes, and then to it's childNodes' nodes, and so on. Basically to all sub-children of the element.

I made a script but it somehow gets lost in an infinite loop.

If someone could help me out, that would be fantastic! :thumbsup:

Here is my script but it's broken, maybe you could fix it or write a simple new one, either would help me out.


//in FF, and maybe others, it returns "\n" & " " as elements...
//so this just checks to see if the tagName property is there,
//thus meaning it has to be a real element (ex: <div>)
//then it returns all the ones it finds as real in a new array
function GetRealChildren(htmlObject){
var realChildElements = new Array();

for(k = 0; k < htmlObject.childNodes.length; ++k){
if(htmlObject.childNodes[k].tagName){
realChildElements.push(htmlObject.childNodes[k]);
}
}
//alert(htmlObject.id + " has " + realChildElements.length + " children");
return(realChildElements);
}

//this is supposed to actually (recursively) add the attributes
//to the elements and it's sub-nodes
function GiveAllRealChildrenAttribute(htmlObj){
var count = 0;
var realChildren = GetRealChildren(htmlObj); //sortof like object.childNodes

//probably only executed (the next 2 lines) if it is the first iteration of recursion
if( htmlObj.getAttribute("theAttributeName) == null){
htmlObj.setAttribute("theAttributeName", "theAttributeValue");
}
else{
//do nothing, just give it to kids then
}

//only gives it to kids
for(j = 0; j < realChildren.length; j++){
//if it has babies, give it the attribute, then all it's babies,
//and it's babies' babies the attribute (recursion)
if(GetRealChildren( realChildren[j] ).length > 0){
realChildren[j].setAttribute("theAttributeName", "theAttributeValue");
count += GiveAllRealChildrenAttribute( realChildren[j] );
}
else{ //if it has no babies, just give it attribute
realChildren[j].setAttribute("theAttributeName", "theAttributeValue");
count += 1;
}
}
return(count);
}

GiveAllRealChildrenAttribute( document.getElementById("the_Element_To_Appy_Attributes_To") );


Comments should explain the basic idea.

Thanks!

Kor
06-10-2008, 01:56 PM
Try this:

function setAttributeToChilds(htmlObj,AttName,AttValue){
var kids=htmlObj.getElementsByTagName('*'), i=0, k;
while(k=kids[i++]){
k.setAttribute(AttName,AttValue)
}
}

hotwheelharry
06-11-2008, 04:05 AM
that works great for the first set of child nodes, but how do you apply it to all sud-nodes of the sub-nodes, and so on?

Kor
06-11-2008, 06:44 AM
Should work for all the nodes, sub-nodes and so on. After all, element.getElementsByTagName('*') refers all the tags nested in an element, not only the direct childNodes tags. As you need to set an attribute to the nodes, that means you are dealing mainly with tags, isn't that so?

hotwheelharry
06-11-2008, 08:13 AM
sorry about my stupidity... :D, I just thought it would get the first "level." Didn't realize the wildcard character also worked... thanks!