I’m trying to translate my jquery code into vanilla js.
this piece of code:
let count = 1;
$($(".card").get().reverse()).each(function () {
$(this).addClass("zIndex" + count);
count++;
});
basically i have a bunch of divs with class .card
, for each of these i need to assign the class zIndex
with a decreasing number, so that it comes out like this:
<div class="card zIndex3"></div>
<div class="card zIndex2"></div>
<div class="card zIndex1"></div>
I’m going to be honest, i have no clue where to start, especially regarding the $($(".card").get().reverse())
any help or even clues on how to proceed is greatly appreciated.
2
Answers
One alternative to
get()
method in jquery isquerySelectorAll
with javascript.And equalized to
reverse
is creating a decreasing loop start from last index of the cards and end with index equal to zero.you can use the array.reverse method in js https://developer.mozilla.org/fr/docs/Web/JavaScript/Reference/Global_Objects/Array/reverse
and to translate the jquery loop you have, you can :
document.querySelectorAll
and[...data]
syntax[...document.querySelectorAll('.card')].reverse()