Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

JavaScript JavaScript and the DOM (Retiring) Traversing the DOM Getting the First and Last Child

Andy Goodman
Andy Goodman
14,207 Points

My code for the additional challenge Guil gave...

See link below for the code I wrote to add/remove the 'up' and 'down' buttons when new items are added/removed or moved.

If anyone is stuck on this, hopefully it can help you. I also removed a lot of repeated code and 'hoisted' the variables to the top of each section.

https://w.trhou.se/c1spg674za

Ian Salmon
Ian Salmon
Courses Plus Student 10,687 Points

This is very well done and gave me some great ideas.

I especially liked..

  • How you created a function using querySelector to define your variables. It really cleaned up the top lines (although I wouldn't personally use $, it's too confusing in my jquery mind).
  • Your solution to hiding the 'up' and 'down' buttons for firstElementChild and lastElementChild was very easy to understand (other students: go to line 77 for the loop).

Thanks for sharing this with the community!

Michel Ribbens
Michel Ribbens
18,417 Points

really good solutions and very clean code! thanks for sharing, this gives me some new ideas on how to structure my code better in the future

1 Answer

Birthe Vandermeeren
Birthe Vandermeeren
17,146 Points

Nice! I had the same idea of hiding the buttons by setting their visibility to "hidden". My function is different though: first it loops through all buttons in the list to set their visibility to "visible". Then, it sets the visibility of the up button of the first item in the list and the visibility of the down button of the last item in the list to "hidden". So, no ifs and else ifs. I've posted it on the forum too, if you're interested.