Cannot get the last element with specific class or attribute using querySelector.
My Current Code:
<div id="wrapper">
<div class="abc" db="box"></div>
<div class="abc" db="box"></div>
<div class="abc" db="box"></div>
<div class="def"></div>
<div class="abc" db="box"></div>
<div class="ghi"></div>
<div class="abc" db="box"></div>
<div class="ghi"></div>
<div class="ghi"></div>
</div>
<script>
console.log(document.querySelector('#wrapper>[db="box"]:last-child')); // showing null
console.log(document.querySelector('#wrapper>div.abc:last-child')); // showing null
</script>
I've found some solutions like:
const elements = document.querySelectorAll('#wrapper>div.abc');
console.log(elements[elements.length - 1]);
The above code works but it gets all of the elements first and then retrieves the last one from it.
document.querySelector('#wrapper>[db="box"]:last-child')
If anyone could make the above query correct then it will search for the last one only.