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

109
Views
showing/hiding html elements with javascript

which option among the following is better or used as a standard way to show/hide the html elements

  1. changing element.style.display
  2. adding/removing a separate class called hide {display: none}
  3. any other standard way

PS: this JavaScript hide/show element question uses the first option mentioned( changes the style to block to show which may not be desired). I would like to know whether this method is used in most websites or the adding /removing a separate class or any other way

  1. A third way in the answers below https://stackoverflow.com/a/68983509/14478972
about 4 years ago · Juan Pablo Isaza
3 answers
Answer question

0

I prefer to toggle a class using DOMTokenList.toggle():

The toggle() method of the DOMTokenList interface removes a given token from the list and returns false. If token doesn't exist it's added and the function returns true.

about 4 years ago · Juan Pablo Isaza Report

0

Option 1 would be standard for only hiding the element, but if you would like to add other styles like transitions and pointer events option 2 is preferred

about 4 years ago · Juan Pablo Isaza Report

0

Well except the first and second, there is the other way. Which is rendering the element its self.

It has a better security. as the user wont know if there is a hidden element inside the toggle div. Eg when people try to look at the html

Have a look below

I used jQuery as its easier to write. If you are not able to rewrite a JavaScript version will be happy to rewrite for you.

var items = $(".toggle");
var item = {};
// setup the auto toggle
$(".toggle").each(function(el) {
  var id = new Date().getUTCMilliseconds() + $(this).index()
  item[id] = $(this).find("content")
  if (!$(this).hasClass("show")){
  $(this).find("content").remove();
  }
   $(this).attr("id", id)
});

$(".toggle").click(function() {
  if ($(this).find("content").length > 0)
    $(this).find("content").remove();
  else $(this).append(item[$(this).attr("id")])

  
})
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<div class="toggle">
  <h1>click here to toggle content </h1>
  <content>
    this is a test
  </content>
</div>

<div class="toggle show">
  <h1>click here to toggle content(start state is visible) </h1>
  <content>
    this is a test
  </content>
</div>

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!