0

I have 3 functions:

window.onload = function() {       
    canvas();   
    changeColor();
    blockSize();    
}

The problem is with third "blockSize()". It not running when I place it at the bottom of window.onlod function; + when I place it at the top, it disables all functions below it. Here is the code (calculates width and height in percentages of all blocks with className "size"):

function blockSize() {
    var block = document.getElementsByClassName("size");    
    for (var i in block) {       
        var width = Math.round(block[i].clientWidth/document.documentElement.clientWidth*100);
        var height = Math.round(block[i].clientHeight/document.documentElement.clientHeight*100);
        var span = document.createElement("span");
        span.innerHTML = width+"x"+height+"%";
        block[i].appendChild(span);
    }    
}

Noticed, that problem appers when I appendChild (last line). When I remove it, other functions works properly. Also tried to put < span> manually inside a div and use this javascript line instead (same results):

block[i].firstChild.innerHTML = width+"x"+height+"%";

!!! When I use only innerHTML, everything works fine. And I understand, that I can use something like this, and it will work:

block[i].innerHTML = "<span>"+width+"x"+height+"</span>";
5
  • 2
    Have you checked the console for errors? Commented Dec 3, 2014 at 16:23
  • Can you post the full code (the other functions) Commented Dec 3, 2014 at 16:24
  • This question appears to be off-topic because it is about a problem which could have been solved if the OP had simply looked at the console. Commented Dec 3, 2014 at 16:35
  • Here is the console code, it didn't tell me, that problem is in loop: Uncaught TypeError: Cannot set property 'innerHTML' of undefined Commented Dec 3, 2014 at 16:45
  • 1
    Basic debugging. Based on the error message, a quick insertion of a console.log(block[i]) or a breakpoint in the debugger to look at things would have show you that you were iterating things you didn't want to iterate and then, at least, you could have included much better info in your question about the problem or perhaps seen yourself what was going on. It's very important to learn basic debugging steps so you can examine what is going on yourself in the affected area. Commented Dec 3, 2014 at 16:47

1 Answer 1

1

The problem most likely comes from improperly using for-in on an HTMLCollection. This will include other items besides DOM elements.

when it encounters the block[i].appendChild(span), it'll throw an error if block[i] isn't an element and therefore doesn't have an .appendChild() method.

Use a for loop instead.

function blockSize() {
    var block = document.getElementsByClassName("size");    
    for (var i = 0; i < block.length; i++) {       
        var width = Math.round(block[i].clientWidth/document.documentElement.clientWidth*100);
        var height = Math.round(block[i].clientHeight/document.documentElement.clientHeight*100);
        var span = document.createElement("span");
        span.innerHTML = width+"x"+height+"%";
        block[i].appendChild(span);
    }    
}
Sign up to request clarification or add additional context in comments.

3 Comments

Thanks, problem is fixed. Didn't expect that problem can be in for-in. For all next times will use only for loop.
@Mark1ra: yeah, in JS when dealing with Arrays or Array-like objects, it's best to use for.
A classic example of why one should never iterate an array with for/in.

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.