Lets say I've got something like this:
<div class='item'>some code</div>
<div class='item current'>some code</div>
<div class='item'>some code</div>
The next element I can get by document.querySelector(".item.current + .item")
. That's great and works fine but.. How can I get to the previous element?
You can get the previous element "sibling" with the previousElementSibling
property. For example
document.querySelector('.item.current').previousElementSibling
Would return the first element, <div class="item"></div>
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With