Right now I'm just getting used to the properties/methods of the TreeWalker object (my curiosity is due to jkd's many posts on the subject). Although, I'm having a problem: I can't read any other nodes besides the root. For instance, I've got a HTML structure like so:
When I add the event listener it works, but when I try to alert the innerHTML of the firstChild node (which is stored in the variable "p") it alerts the value "undefined".
I'm not sure of the problem so I was hoping someone else could help me solve this problem.
Happy coding!
Code:
<html> <head> <title>DOM TreeWalker</title> <script type="text/javascript"> function walkTree() { var tw=document.createTreeWalker(document.body,1,null,true); var body=tw.currentNode; var p=body.firstChild; body.addEventListener("click",new Function("alert('event fired');"),false); alert(p.innerHTML); } </script> </head> <body onload="walkTree();"> <p id="myParagraph">my paragraph</p> </body> </html>
I'm not sure of the problem so I was hoping someone else could help me solve this problem.
Happy coding!

Comment