+ 1
How to order html elements with JS
Ok, letâs say I got 3 elements in the HTML - all with the same class and also different class: Class = âa oneâ; Class = âa twoâ; Class = âa threeâ; CSS: .a{ position: absolute; } Now if I wanted to order them by Z-index or transformZ(#px), I could use nth-child(), but imagine if I had 100 of those elements - that would take a long time to do. So how would I do it in JS? Iâm thinking weâd start like this: var elms = doc...selectorAll(â.aâ); var i = 0; Then i++ or i+= #; I donât know how to finish that
3 RĂ©ponses
0
then loop trough the collection with for of loop
for element of elms{
//work here
}
0
Taste well, ya loops.
but what would the actual code look like?
-
ok, we start with getting the elements...
var elms = document.querySelectorAll(â.aâ);
var i = 0;
...
elms.style.zIndex = i++;
..
0
pretty close yeah