Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

shaungbhone's avatar

Javascript DOM

Hello folks, please help me. I was stucking the whole day. Thank you for your kindness.

Question is!.

After adding laravel livewire pagination, my javascript DOM :last-child gives me null. Why?

const lastCard = document.querySelector('.parent);
it's working.
const lastCard = document.querySelector('.parent:last-child);
give me null
0 likes
1 reply
LaryAI's avatar
LaryAI
AI
Best Answer
Level 58

The reason why document.querySelector('.parent:last-child') is returning null is because :last-child is a CSS pseudo-class that selects the last child element of its parent. It does not work with querySelector in the way you are trying to use it.

To select the last element with the class .parent, you can use document.querySelectorAll('.parent') to get all elements with that class, and then select the last one using array indexing. Here's an example:

const cards = document.querySelectorAll('.parent');
const lastCard = cards[cards.length - 1];

This will give you the last element with the class .parent.

Please or to participate in this conversation.