JavaScript efficient style change on the entire class of elements

Ulrik

In an example of a very common scenario, where we need to change the style of an entire class of elements, we have a (simplified and generalized) code that looks like this:

var elements = document.getElementsByTagName('div');
for (var i = 0; i < elements.length; i++)
    if (elements[i].className == 'HideMe')
        elements[i].style.visibility = 'hidden';

It gets all the div elements from the document, loops through them, and changes the visibility of ones that have class "HideMe" to "hidden". On the other hand, this code:

document.innerHTML.replace(/class="HideMe"/mg, 'class="HideMe" style="visibility: hidden"');

will insert the the invisibility style to everything that has class "HideMe". I am new to JavaScript, and don't get me wrong, but every example, every tutorial that I've seen so far, teaches the first method. Shouldn't a one-liner, a single function call, a replace algorithm created by a more intelligent being, be faster and less resource-intensive than any kind of loop wiith an if statement? The question is actually more general one, why not do:

document.innerHTML.replace('id="foo"', 'id="bar"');

instead of:

document.getElementById('foo').id = 'bar';

The inspected code is absolutely the same, but for the performance, I would probably need to make it change the style of thousands of elements so I can measure any significant difference. Are there any good, well-argued reasons why we should favor one method over the other?

dfsq

Regexp solution is very bad and should never be used (except maybe in very specific cases). The main reason why is that you don't want to replace innerHTML of the body. What happens in this case is that entire DOM tree has to be rebuild and rerendered, causing not only UX lags and potential performance issues but which is more important - loss of all bound event handlers.

On the other hand, proper usage of DOM manipulation methods should give you the best performance and cleaner and easier to read code.

However, the first method you mention is also not very good: you should avoid changing CSS styles with Javascript. Ideal approach would be to add one more modifier class to elements, for example:

var elements = document.getElementsByTagName('div');
for (var i = 0; i < elements.length; i++)
    if (elements[i].className == 'HideMe')
        elements[i].className += ' HideMe-hidden';
        // or elements[i].classList.add('HideMe-hidden');

where in CSS you would have

.HideMe-hidden {
    visibility: hidden;
}

This is the most flexible and optimal solution.

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

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

Change css class style with javascript

can't change style all elements with javascript

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

How to change a css class style through Javascript?

JavaScript: change style of element specified by class

Pure JavaScript - how to add class to a class? Or change style in class

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

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

Only change single elements style from many similar class selector

Javascript DOM detect elements style change (Without JQUERY)

How to use javascript to automatically grab the height of elements to change the CSS style?

Changing style of all same class name of elements using javascript

javascript, can't change class on all elements of the same class

change style of closest class when input radio is selected - Javascript

JavaScript not changing style of elements

How to change style of AppBarLayout in the entire theme?

Javascript: Change elements that have certain class and attribute -> pure Javascript

Disable the style that covers the entire img element in a class

Is there a more efficient way to change the style of the clicked buttons?

Change style of pseudo elements in angular

Javascript: change color of the entire page

Change style from javascript

Change content of all elements having same class using javascript

How to change attribute to a whole class of elements using jQuery or JavaScript?

Change class of multiple elements

I want to change style of elements with same class name, by loading external .js

Change style in CSS by character class