Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

108
Vistas
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 Respuestas
Responde la pregunta

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 Denunciar

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 Denunciar

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 Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda