0

I've got a bug when I'm using a javascript function : my function displays the content of a div element but when I uncomment some code it doesn't work anymore. Someone has any idea why ?

    function traverse(){
        var root=document.getElementById('tree0').childNodes;
        for(var i=0;i<root.length;i++) {
                var lis = root[i];
                var number =0;
                for (var member in lis) {
                    output.innerHTML+=lis[member];
                    /*var assertion = lis[member];
                    var result = assertion.indexOf("Bookmarks menu");
                    if(result != -1) {
                       output.innerHTML+='Test';
                    }*/

                 }
         }
     }

thanks,

Bruno

2
  • @Raynos I don't know (lis[member].type = undefined) Commented Apr 19, 2011 at 12:33
  • Thanks everyone for answering but I mispelled only when I copied the source code so the error is not here. Commented Apr 19, 2011 at 12:38

3 Answers 3

4

You may get more that you expect when you do for ... in ...

Is THIS what you want? http://jsfiddle.net/bM9Bn/

<div id="tree0">
  <div id="bla">
    bla
  </div>
  <div id="Bookmarks menu">
    Bookmarks Menu
  </div>
</div>
<hr />
<div id="output"></div>
<script>
var output = document.getElementById("output")
function traverse(){
  var root=document.getElementById('tree0').childNodes;
  for(var i=0;i<root.length;i++) {
    var lis = root[i];
    var number =0;
    for (var member in lis) {
//      output.innerHTML+="["+member+":"+lis[member]+"]";
      if (member == "id" || member == "textContent") {
        output.innerHTML+="["+member+":"+lis[member]+"]";
        var assertion = lis[member];
        // the typeof test not needed if we only process textContent and ID
        if (typeof assertion == "string") { 
          var result = assertion.indexOf("Bookmarks menu");
          if(result != -1) {
            output.innerHTML+='<span style="color:red">Test</span>';
          }
        }
      }
    }
  }
}
traverse()
</script>     
Sign up to request clarification or add additional context in comments.

3 Comments

@Bruno: What are you looking for. can we please see what the tree looks like. What in a node list can be the string "Bookmars menu" - I would expect asserion.innerHMTL or assertion.textContent or something
+1 from me. Dunno what the OP wants, but expecting all enumerable properties of a DOM element to be strings is not sensible. Searching through every property looking for a specific string doesn't make sense, surely the OP knows which property is relevant?
@RobG -yes, a document.getElementsByTagName or similar
0

Just looking quickly- var and if statement should both us resultat?

var resultat = assertion.indexOf("Bookmarks menu"); if(result*at* != -1) {

Comments

0

You are checking the 'result' variable, but setting the 'resultat' variable. Try using resultAt in the conditional, and I think it will work for you.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.