For loop in Javascript runs only once

Posted by user592748 on Stack Overflow See other posts from Stack Overflow or by user592748
Published on 2013-10-18T15:58:07Z Indexed on 2013/10/20 21:54 UTC
Read the original article Hit count: 212

Filed under:
|

Here is my code. I do not quite understand why the for loop runs only once, both inner and outer. nodeList.length and innerNodeList.length show appropriate values when I generate alert messages. I see that both i and j do not increment beyond 0. Kindly point out anything wrong with the code.

function getCategoryElements() {
var newCategoryDiv = document.getElementById("category");
var nodeList = newCategoryDiv.childNodes;

for (var i = 0; i < nodeList.length; ++i) {
    var innerNodeList = nodeList[i].childNodes;
    alert("innerNodeList Length" + innerNodeList.length.toString());

    for (var j = 0; j < innerNodeList.length; ++j) {
        if (innerNodeList[j].nodeName == "SELECT") {
            alert("inside select Node value " + innerNodeList[j].nodeValue.toString());
            document.getElementById("newCategories").value = 
                document.getElementById("newCategories").value + '<%=delimiter%>' + innerNodeList[j].nodeValue;
        } else if (innerNodeList[j].nodeName == "TEXTAREA") {
            document.getElementById("newCategoriesData").value =
                document.getElementById("newCategoriesData").value + '<%=delimiter%>' + innerNodeList[j].nodeValue;
        }
    }
  }
}

© Stack Overflow or respective owner

Related posts about JavaScript

Related posts about for-loop