Click here to Skip to main content
15,903,030 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I have a treeview,in which when iam selecting a child node..iam getting the text of child node as alert message using

var obj = window.event.srcElement;
         if (obj.tagName == "A")
{
          alert(obj.innerHTML);
}


Now I want the full heirarchy as ParentNode->SubNode->ChildNode in an alertmessage in javascript..
Posted
Updated 19-Jul-10 3:34am
v2

Try something like:

C#
function traverseElements(elem)
{
  alert(elem.innerHTML);
  for (i=0; i<elem.elements.length; i++)
  {
    traverseElements(elem.elements(i));
  }
}



Good luck!
 
Share this answer
 
v2
Thanks for the reply...But still that is not comming in heierachy
 
Share this answer
 
You would just have to concatenate everything and add an indent parameter, something like the code below.

C#
function test()
{
  alert(traverseElements(startElement, ''));
}


function traverseElements(elem, indent)
{
  var result = indent + elem.innerHTML + '\n';
  for (i=0; i<elem.elements.length; i++)
  {
    result += indent + traverseElements(elem.elements(i), indent + ' ');
  }
  return result;
}


Good luck!
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900