Best way to hide/unhide hundreds of DOM elements: change STYLE or CLASS?

Inigo

two ways

There are two common ways to dynamically change the visibility of an element with javascript...

By modifying its style:

// occupies same space when hidden
elem.style.visibility = 'hidden' // 'visible' to unhide

// does not occupy any space when hidden
elem.style.display = 'none' // 'block' | 'inline' to unhide

By modyfying its classList:

elem.classList.add('hidden') // classList.remove('hidden') to unhide
.hidden {
    visibility: hidden; /* occupies same space when hidden */
    /* display: none;   /* does not occupy any space when hidden */
}

ℹ️ There are other properties that can be used such as opacity (See @Kingfish's comment), but they all must still be updated by either modifying their style or their classList.

but which is better for hiding/unhiding large numbers of elements?

For one or a few elements, modifying style or classList will work the same. But I need to change the visibility of dozens or even hundreds of elements. For volume changes, Which methods is better in terms of the following?

  • minimizing CPU usage
  • minimizing redraw latency
  • minimizing UI thread blocking

I have a very fast computer so it is hard to know the impacts on people with more performance constrained devices.

is there a third way?

All of the elements I want to show/hide in unison have a specific class. It seems most logical that I should be able to modify the CSS rules for that class, i.e. to set its CSS visibility property, rather than add/remove another class for each of the hundreds of members of the class. I've researched this and few people talk about it, which makes me wonder if it is considered a hack, not-compatible across browsers, or bad for some other reason.

Is there a well-founded explanation one way or the other?

Bergi

Is there a third way?

Yes, there is. There's even a fourth way.

All of the elements I want to show/hide in unison have a specific class. It seems most logical that I should be able to modify the CSS rules for that class, i.e. to set its CSS visibility property, rather than add/remove another class for all of these elements.

Yes, this is possible, using the CSS Object Model. Get the styleheet defining the rule, get the rule for that class, change its definition.

It's not a hack, and has good browser support. It's rarely done because few people know about it, and because accessing the right rule is a bit fiddly (which you can work around by creating and inserting the rule object using CSSOM in the first place). I'm not certain how well-optimised it is - but it certainly needs less JS processing than to alter each individual element.

However, there's a much easier solution: use cascading style sheets!

body.hide-x .x {
    display: none; 
}
/* or, reverse:
.x {
    display: none;
}
body.show-x .x {
    display: inline;
}
*/
document.body.classList.toggle('hide-x');

This will show/hide all elements with class x inside the document, based on whether the show-x class is applied on the body or not.

You can replace display: none with visibility: hidden or opacity: 0 or any other property change you want to apply en masse depending on your needs.

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

Change CSS style of a class with javascript, but not in the DOM

JavaScript efficient style change on the entire class of elements

Javascript DOM detect elements style change (Without JQUERY)

React.js -- Functional Components -- DOM manipulation, change the style of elements

How to target DOM and change style in react with mapped elements

Best way to change background color with JavaScript HTML DOM

Best way to style markdown

Best way to style a component?

jQuery counting elements by class - what is the best way to implement this?

Best way to construct an object class with a collection array of three elements

Angular 6 + get elements with class name from document and change style

find elements by class whose id contains a substring and change the style

Change class style (not individual elements) that's not in stylesheet using Javascript

Only change single elements style from many similar class selector

What is the best way for (potentially) hundreds of mobile clients to access a MySQL database?

Best way to change FontAwsome stars class regular to solid

Best way to change class of column of data frame in R

Best way to store, change, and save large number of class objects

What is the best way to change the URL onClick or onSubmit in a React Class Component?

Best way to style cakephp app

Looping through DOM elements and children to create a JS Array - having trouble understanding the best way to accomplish this

Cannot style elements using DOM manipulation when new class is added by element.classList.add() method in javascript

Using Observables for streams in Angular and subscribing to them in another class what is the best way to approach delivery to the DOM/template

Best way to hide duplicate elements

Best way to arrange elements in div

Change style of pseudo elements in angular

Smooth way to repeat DOM elements

React - What is the best way to change an elements text each time the user clicks anywhere on screen?

Accessing Style of Ionic 2 DOM Elements