How to get the first and last elements with JavaScript querySelector?

Spread the love

To get the first and last elements with JavaScript querySelector, we use the first-child and last-child selectors.

For instance, we write

const first = div.querySelector("[move_id]:first-child");
const last = div.querySelector("[move_id]:last-child");

to call querySelector with a select with first-child at the end to select the first element with attribyute move_id.

And we call querySelector with a select with last-child at the end to select the first element with attribyute move_id.

Leave a Reply

Your email address will not be published. Required fields are marked *