Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

232
Views
Save a variable number of CSS styles as a JS variable for show/hide?

Let me preface by saying I don't have a ton of Javascript experience. I have a CSS class specifically used to show/hide things with a button click on my site. The progress bars are shown by default, and have their various colors and styles shown no problem. Using the following code, the button will hide the progress bars correctly.

    function showhide() {
    var x = document.getElementsByClassName("showhideclass");
    var i;
    for (i = 0; i < x.length; i ++) {
      if (x[i].style.display === "none") {
        x[i].style.display = "block";
      } else {
        x[i].style.display = "none";
      }
    }
  }

However, when the button is clicked again to show the progress bars again, the color and other styling does not show up. I know this is because of the line x[i].style.display = "block"; but I do not know how to save the CSS styling as a variable in this function to have it reapplied on "Show" click. I've tried the following code, and it will still hide the progress bars properly, but it does not display them again on second click.

function showhide() {
    var x = document.getElementsByClassName("showhideclass");
    var i;
    for (i = 0; i < x.length; i ++) {
      var elstyle = window.getComputedStyle(x[i]);
      if (x[i].style.display === "none") {
        x[i].style.display = elstyle;
      } else {
        x[i].style.display = "none";
      }
    }
  }

I'm not quite sure what I'm missing here, does anyone have suggestions?

about 4 years ago · Juan Pablo Isaza
2 answers
Answer question

0

Maybe you want to use something like this.

Define some css class with a display: none property and toggle the class on the elements itself via button-press. All properties such as display: flex, display: block, or display: none will not be overwritten.

function showhide() {
  const elements = document.getElementsByClassName("showhideclass");
  [...elements].forEach(e => e.classList.toggle("hidden"));
}
.hidden {
  display: none;
}
<p class="showhideclass">1</p>
<p class="showhideclass">2</p>
<p class="showhideclass">3</p>
<p class="showhideclass">4</p>
<button onclick="showhide()">Toggle</button>

about 4 years ago · Juan Pablo Isaza Report

0

@jns had the right of it. The following code works perfectly as intended, with the styles being saved for each progress bar as it is hidden and shown.

  function showhide() {
    var x = document.getElementsByClassName("showhideclass");
    var i;
    for (i = 0; i < x.length; i ++) {
      var elstyle = window.getComputedStyle(x[i]);
      if (x[i].style.display === "none") {
        x[i].style = elstyle;
        //x[i].style.display = "block";
      } else {
        x[i].style.display = "none";
      }
    }
  }

Edit: the bot yelled at me, so I will further explain. Changing the line x[i].style.display = elstyle; resolved the issue; removing .display causes this line to apply the saved style AS a style, instead of as the display property.

about 4 years ago · Juan Pablo Isaza Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!