How to order html elements with JS | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
+ 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

7th Dec 2019, 4:11 PM
Ginfio
Ginfio - avatar
3 Answers
0
then loop trough the collection with for of loop for element of elms{ //work here }
7th Dec 2019, 5:12 PM
Taste
Taste - avatar
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++; ..
8th Dec 2019, 2:45 AM
Ginfio
Ginfio - avatar
0
pretty close yeah
8th Dec 2019, 2:57 AM
Taste
Taste - avatar