Home > Software engineering >  How to use .classList.add("hide"); to hide curtains when viewing a 2nd time
How to use .classList.add("hide"); to hide curtains when viewing a 2nd time

Time:10-07

Curtains stay hidden when .setAttribute("hidden", true); is being used. https://jsfiddle.net/g2th3opc/

evt.currentTarget.closest(".inner-container").querySelector(".sliding-panels").setAttribute("hidden", true);

Curtains don't stay hidden when .classList.add("hide"); is being used. https://jsfiddle.net/06vdjka4/

evt.currentTarget.closest(".inner-container").querySelector(".sliding-panels").classList.add("hide");

To reproduce: Click 1 svg play button, then click the X.

Do that repeatedly, click on all of them, even the ones you already clicked on.

You will notice that the curtains don't stay hidden when .classList.add("hide"); is being used.

The curtains are supposed to stay hidden after clicking on the same svg play button a 2nd time.

Why don't they stay hidden, and is there a fix for that?

I tried to post a snippet but it went over the character limit.

CodePudding user response:

Your issue is the way you are showing the buttons:


  function showAllButtons(buttonSelector) {
    const allButtons = document.querySelectorAll(buttonSelector);

    function showButton(button) {
      // This is where the hide class is getting removed
      button.classList.remove("hide");
    }
    allButtons.forEach(showButton);
  }

  function resetPage() {
    resetBackground("body");
    resetCurtains(".with-curtain");

    // You aren't just matching buttons with this selector. You are matching everything that has been hidden!
    showAllButtons(".hide");

    resetButtons(".outer");
  }

  // This is called whenever an exit button is clicked
  function exitClickHandler(evt) {
    resetPage(); // During this function calls all other .sliding-panels divs are show
    // This only hides the current video's .sliding-panels div
    evt.currentTarget.closest(".inner-container").querySelector(".sliding-panels").classList.add("hide");
  }

The solution is to be more specific with your selector for un-hiding buttons:

    // Notice the increased specificity of this selector:
    showAllButtons(".container.hide");

CodePudding user response:

Just do this small fix to exit handler.

function exitClickHandler(evt) {
resetPage();

evt.currentTarget
    .closest(".inner-container")
    .querySelector(".sliding-panels")
    .removeAttribute("hidden"); //remove hidden attribute on exit
 }

https://jsfiddle.net/h9yfd1nv/

  • Related