1

I'm getting w3c validation errors with this Javascript code and wonder if a kind gent/lady would spare me a moment to take a gander.

// hide all element nodes within some parent element
function hideAll(parent) {
    var children = parent.childNodes, child;
    // loop all the parent's children
    for (var idx=0, len = children.length; idx<len; ++idx) { /* ERROR HERE */
        child = children.item(idx);
        // if element node (not comment- or textnode)
        if (child.nodeType===1) {
            // hide it
            child.style.display = 'none';
        }
    }
}

The Errors are:

  • element "len" undefined
  • character ";" not allowed in attribute specification list

The semi-colon at idx<len; is where it goes wrong.

Can someone explain where I'm going wrong with the above code snippet?

Many thanks.

2 Answers 2

1

Change it to:

// hide all element nodes within some parent element
function hideAll(parent) {
    var children = parent.childNodes, child;
    // loop all the parent's children
    var len = children.length;
    for (var idx=0; idx<len; ++idx) { /* ERROR HERE */
        child = children.item(idx);
        // if element node (not comment- or textnode)
        if (child.nodeType===1) {
            // hide it
            child.style.display = 'none';
        }
    }
}
Sign up to request clarification or add additional context in comments.

3 Comments

Thanks, Rick. However, I'm still getting the exact same validation errors.
Validation highlights the semi-colon here: idx<len;
I've moved it out of my HTML which fixes it. It was just in case it was a known issue.
1
          **// hide all element nodes within some parent element



             function hideAll(parent) 
             {
                var children = parent.childNodes, child;

                // loop all the parent's children
                var len=children.length;

                 for (var idx=0; idx<len; ++idx) 
                 { /* ERROR HERE */

                   child = children.item(idx);

                    // if element node (not comment- or textnode)

                     if (child.nodeType===1) 
                     {
                         // hide it
                         child.style.display = 'none';
                     }
                } 
         }**

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.