How to Check if Event Target is the Last Child in JavaScript
How can we check if an event target is the last child in JavaScript?
Suppose we have an event listener that calls the following handler()
function.
const handler = (e) => {
const element = e.target;
// Check if element is last child
}
We can easily check if an element is the last child by checking if it has a sibling after it, or a nextSibling
.
const handler = (e) => {
const element = e.target;
if (!element.nextSibling) {
// It is the last child
}
}