Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

115
Visualizações
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 Respostas
Responde à pergunta

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 Relatório

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 Relatório

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 Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda