2

Need help with the following JavaScript function.

when I isolate each as if they were separate functions (i.e. to remark out all but one at a time). I do get the expected value alerted.

But when run as block of code only the first value is alerted. Not able to see what I'm doing wrong

function stateChanged(){ 

    document.getElementById("txtHint").innerHTML=xmlhttp.responseText;            

    if (xmlhttp.readyState==4){ 

        var product = document.getElementById ("product");
            for (var i = 0; i < product.childNodes.length; i++) {
                var child = product.childNodes[i];
                if (child.nodeType == 3) {
                     //alert ("The " + i + ". child is a text node. Contents:\n" + child.data);
                    var customerProduct =  product.childNodes[i].nodeValue;
                    alert(customerProduct);   
                }
                else {
                    if (child.nodeType == 1) {
                        var product =  ("The " + i + ". child is an element node. Contents:\n" + child.innerHTML);
                    }
                }
            }

         var model = document.getElementById ("model");
            for (var i = 0; i < model.childNodes.length; i++) {
                var child = model.childNodes[i];
                if (child.nodeType == 3) {
                     //alert ("The " + i + ". child is a text node. Contents:\n" + child.data);
                    var productModel =  model.childNodes[i].nodeValue;
                    alert(productModel);   
                }
                else {
                    if (child.nodeType == 1) {
                        var model =  ("The " + i + ". child is an element node. Contents:\n" + child.innerHTML);
                    }
                }
            }

         var serial = document.getElementById ("serial");
            for (var i = 0; i < serial.childNodes.length; i++) {
                var child = serial.childNodes[i];
                if (child.nodeType == 3) {
                     //alert ("The " + i + ". child is a text node. Contents:\n" + child.data);
                    var productSerial =  serial.childNodes[i].nodeValue;
                    alert(productSerial);   
                }
                else {
                    if (child.nodeType == 1) {
                        var serial =  ("The " + i + ". child is an element node. Contents:\n" + child.innerHTML);
                    }
                }
            }

         var computer = document.getElementById ("computer");
            for (var i = 0; i < computer.childNodes.length; i++) {
                var child = computer.childNodes[i];
                if (child.nodeType == 3) {
                     //alert ("The " + i + ". child is a text node. Contents:\n" + child.data);
                    var computer =  computer.childNodes[i].nodeValue;
                    alert(computer);   
                }
                else {
                    if (child.nodeType == 1) {
                        var computer =  ("The " + i + ". child is an element node. Contents:\n" + child.innerHTML);
                    }
                }
            } 

         var os = document.getElementById ("os");
            for (var i = 0; i < os.childNodes.length; i++) {
                var child = os.childNodes[i];
                if (child.nodeType == 3) {
                     //alert ("The " + i + ". child is a text node. Contents:\n" + child.data);
                    var os =  os.childNodes[i].nodeValue;
                    alert(os);   
                }
                else {
                    if (child.nodeType == 1) {
                        var os =  ("The " + i + ". child is an element node. Contents:\n" + child.innerHTML);
                    }
                }
            }
    } 
 }

appreciated. ussteele

demongolem
  • 9,474
  • 36
  • 90
  • 105
ussteele
  • 133
  • 1
  • 3
  • 10
  • 1
    You do not understand JavaScript closures. See the [Jibbering JavaScript Closure Notes](http://jibbering.com/faq/notes/closures/) and/or other SO questions. I have given you one for free :-) –  Nov 07 '10 at 20:44
  • 1
    possible duplicate of [How does a javascript closure work ?](http://stackoverflow.com/questions/111102/how-does-a-javascript-closure-work) –  Nov 07 '10 at 20:44
  • 1
    Well I don't see the closure in this one. Could you enlighten us pst? – Bitsplitter Nov 07 '10 at 21:04
  • respondents. Thank you for your help in direction. – ussteele Nov 07 '10 at 21:04
  • Closures are good to know about anyway. But the problem at hand... I don't see the problem, perhaps there's a prob with the multiple var declaration of 'child'? – danjah Jun 22 '11 at 01:58

0 Answers0