1

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;
        }
    }
  }
}
9
  • 5
    Can you build a fiddle ? Commented Oct 18, 2013 at 16:00
  • console.log the nodeList.length and innerNodeList.length. Im sure youll find that they are both equal to 1. Make a fiddle or post your HTML so we can see how the category div is set up. Commented Oct 18, 2013 at 16:02
  • Why are you putting var i outside loop statement? You can do: for (var i = 0; i < nodeList.length; ++i) {....} Commented Oct 18, 2013 at 16:04
  • 1
    we'll also need more context - the error could be something as basic as the fact that your array only has 1 element. Commented Oct 18, 2013 at 16:04
  • @NickL. Really? That makes no difference. :) Commented Oct 18, 2013 at 16:13

1 Answer 1

1
var newCategoryDiv, nodeList, innerNodeList, innerNode, i, j;

newCategoryDiv = document.getElementById("category");
nodeList = newCategoryDiv.childNodes;

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

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

I took the liberty to refactor your code and clean it up a little bit. In case you're not aware, all variables have function scope in Javascript, so no matter where you declare them within a single function, Javascript treats them as if the variable declaration is the first statement.

It appears that your code is syntactically correct, and so I think that the most logical place to look for a problem is that there could be an error occurring after the last alert function call.

In order to check this, try adding another alert function call to the end of the inner loop. If it doesn't run, you'll know this is the case.

Sign up to request clarification or add additional context in comments.

1 Comment

Thanks. This helps. The last alert was not appearing. I looked at console under developer tools, there were perhaps 2 errors. toString was throwing up error as @epascarello pointed out. Also, '<%=delimiter%>' was throwing an error. When both corrected, the error was that node.value was null. Hence, I used <portlet:namespace /> to retrieve both elements.

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.