0
function toggle() {
    overlays = document.getElementsByClassName("overlay");

    if (globalOpacityValue == 0.9){
        globalOpacityValue = 0;
    }else{
        globalOpacityValue = 0.9;
    }
    console.log("globalOpacityValue: " + globalOpacityValue);

    for (overlay in overlays) {
        overlays[overlay].style.opacity = globalOpacityValue;
    }
    console.log("toggle routine done!");

    tempStoreOnChangeArtificial();
    console.log("tempStoreOnChangeArtificial done!");
}

It seems to me that after the for/in loop, my function ends.

My toggle() never display "toggle routine done!", and obviously it doesn't run the tempStoreOnChangeArtificial();

But it does things in the for/in loop.

why?

4
  • 4
    Probably you're getting errors. Please share a Minimal, Complete, and Verifiable example Commented Feb 14, 2018 at 21:28
  • 1
    Your for loop is likely not running properly. In overlays[overlay].style.opacity = globalOpacityValue;, there is a chance that overlays[overlay] is undefined. Then you are trying to access the style property of undefined, which will throw an error. Commented Feb 14, 2018 at 21:30
  • So normally for/in loop doesn't ends a function? If that's the case I should run through my other codes. Commented Feb 14, 2018 at 21:30
  • Patrick Michaelsen: no. The things in the for/in loop is being done. But yeah, the console always said it can't access to style of undefined. But it still change the style normally so I didn't care Commented Feb 14, 2018 at 21:31

3 Answers 3

2

document.getElementsByClassName returns an array like object, which is not iterable with a for ... in statement.

function toggle() {
    var overlays = document.getElementsByClassName("overlay"),
        i;

    if (globalOpacityValue == 0.9){
        globalOpacityValue = 0;
    }else{
        globalOpacityValue = 0.9;
    }
    console.log("globalOpacityValue: " + globalOpacityValue);

    for (i = 0; i< overlays.length; i++) {
        overlays[i].style.opacity = globalOpacityValue;
    }
    console.log("toggle routine done!");

//    tempStoreOnChangeArtificial();
    console.log("tempStoreOnChangeArtificial done!");
}

var globalOpacityValue = 0;
toggle();
<div class="overlay">a</div>
<div class="overlay">b</div>
<div class="overlay">c</div>

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

Comments

1

I'm not aware that you can iterate over document.getElementByClassName using for in perhaps that's where the problem is, you may want to iterate over HTMLCollection like below:

function toggle() {
    overlays = document.getElementsByClassName("overlay");

    if (globalOpacityValue == 0.9){
        globalOpacityValue = 0;
    }else{
        globalOpacityValue = 0.9;
    }
    console.log("globalOpacityValue: " + globalOpacityValue);

    Array.prototype.forEach.call(overlays, function(overlay) {
        overlay.style.opacity = globalOpacityValue;
    });

    console.log("toggle routine done!");

    tempStoreOnChangeArtificial();
    console.log("tempStoreOnChangeArtificial done!");
}

Comments

1
  • You're trying to get an element from HTMLCollection using in.
  • Use overlay of overlays.

function toggle() {
    overlays = document.getElementsByClassName("overlay");

    if (globalOpacityValue == 0.9){
        globalOpacityValue = 0;
    }else{
        globalOpacityValue = 0.9;
    }
    console.log("globalOpacityValue: " + globalOpacityValue);

    for (overlay of overlays) {
        overlay.style.opacity = globalOpacityValue;
    }
    console.log("toggle routine done!");

    tempStoreOnChangeArtificial();
    console.log("tempStoreOnChangeArtificial done!");
}

Resources

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.