JavaScript JavaScript and the DOM Traversing the DOM Getting the First and Last Child

Cornelius Hager
Cornelius Hager
5,653 Points

Solving the challenge with 3 lines of CSS

I know, a little bit besides the point of the challenge. But very effective and efficient:

.list ul li:first-child button.up, .list ul li:last-child button.down{
    display: none;
}

1 Answer

Steven Parker
Steven Parker
195,561 Points

I like this a bit better since it doesn't disturb the layout as much:

.list li:first-child button.up, .list li:last-child button.down {
    visibility: hidden;
}

And notice that "ul" isn't needed in the selector (it's implied by "li")

But this is kind of "cheating", since this course was all about DOM manipulation with JavaScript. So another technique that I like even better (and not possible with CSS) is to disable the unusable buttons but leave them visible.

document.querySelector(".list li:first-child button.up").disabled = true;
document.querySelector(".list li:last-child button.down").disabled = true;