Using querySelectorAll to change the style property of multiple elements

user2840467 :

I have the following function which when triggered will make a DIV become semi-transparent.

function changeOpacity(el) {
    var elem = document.getElementById(el);
    elem.style.transition = "opacity 0.5s linear 0s";
    elem.style.opacity = 0.5;
}

However I would like this function to apply to several DIVs simultaneously. I tried giving each DIV the same class name and then using getElementsByClassName but couldn't figure out how to implement it.

Would querySelectorAll be more appropriate and if so how would I implement it?

AtheistP3ace :

I would select them with a querySelectorAll and loop over them.

function changeOpacity(className) {
    var elems = document.querySelectorAll(className);
    var index = 0, length = elems.length;
    for ( ; index < length; index++) {
        elems[index].style.transition = "opacity 0.5s linear 0s";
        elems[index].style.opacity = 0.5;
    }
}

Edit: As a comment said above you might be better off putting these values in a class if they are not dynamic and use:

elems[index].classList.add('someclass');

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

How to change elements selected using querySelectorAll

How to change style of multiple elements with a single script?

Using querySelectorAll to target nested elements

How can I make "style.property" work on multiple elements?

Selecting multiple elements with querySelectorAll and applying event in JavaScript

Iterate through multiple elements of querySelectorAll with forEach

Is querySelectorAll a good way to select elements in multiple charts?

Change attribute of multiple elements on hover by using CSS

Using nth-of-type to change the style of child elements

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

How to remove elements that were fetched using querySelectorAll?

querySelectorAll root elements without using :scope

add style to ALL child elements using plain javascript (multiple containers)

How to toggle display style using vanilla JS on multiple elements

Using querySelectorAll to dynamically change the value on an input

Updating multiple element styles using querySelectorAll

Change style of multiple element groups simultaneously on hover using Angular

How to change style for multiple views in Sharepoint 2019 on premise using Powershell

Change style of pseudo elements in angular

Change class of multiple elements

On hover change multiple elements

Randomly change background image for multiple elements using jquery

How to change the attributes values of multiple cloned HTML elements using jQuery?

how to change the dimensions of multiple backgrounds of a single div elements using jquery

cannot change css property of desired amount of elements using eventListener due to elements being created by javascript

Using querySelectorAll to get ALL elements with that class name, not only the first

Get elements by attribute when querySelectorAll is not available without using libraries?

I cannot get access to all desired elements using querySelectorAll

Removing elements from dom using queryselectorall, jquery, getelementsbyid, getelementsbyclassname