0

I am using a JavaScript file to hide various div's all with the same class name, the program gets as far as hiding them but when I want them to be visible after "x" number of seconds the array of elements becomes undefined and has length 0, not sure what is going on. I have tried changing "lowerDash" to a global variable to no avail.

function newBaseHandler(){

    if (document.getElementById("Base6").innerHTML == `<br><p>Create new base</p>`) {
        let lowerDash = document.getElementsByClassName("LowerDashboard");
        let message = document.getElementById("Message");
        for (let button of lowerDash) {
            button.style.visibility = "hidden";
        }

        message.innerHTML = `<br><p>Base created successfully</p>`;

        setTimeout(function() {
            message.innerHTML = ``;
            console.log(lowerDash.length);
            for (let button of lowerDash) {
                button.style.visibility = "visible";
            }
        }, 1000);

    }

}
John Costa
  • 11
  • 1
  • 2
  • 1
    can you show your HTML please. – Jaromanda X Dec 12 '19 at 23:08
  • Both your loops hide the elements. You're never setting `visibility` back to `visible`. Also, **[don't use `.getElementsByClassName()`](https://stackoverflow.com/questions/54952088/how-to-modify-style-to-html-elements-styled-externally-with-css-using-js/54952474#54952474)** - especially when looping! – Scott Marcus Dec 12 '19 at 23:19
  • @ScottMarcus sorry, copy and paste mistake my code has it as visible. What elso should I use instead of .getElementByClass? – John Costa Dec 12 '19 at 23:33
  • 1
    what does `console.log(lowerDash.length);` output - please show your HTML – Jaromanda X Dec 12 '19 at 23:46
  • Read the link I shared and you'll see what the proper alternative is. – Scott Marcus Dec 13 '19 at 02:11

1 Answers1

0

If you want them to become visible again after a timeout, try

button.style.visibility = "visible"

in your for loop insie setTimeout

Кatya
  • 447
  • 1
  • 5
  • 13