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

Hosung Kim
Hosung Kim
11,243 Points

Challenge Solution

My solution was to create a function that loops through the list items and check if each item has a previousElementSibling or nextElementSibling (boolean value). If they do not, then hide the 'up' and 'down' buttons respective to whether they are first or last on the list. Then call the function on page load and/or button press.

const toggleList = document.getElementById('toggleList');
const listDiv = document.querySelector('.list');
const descriptionInput = document.querySelector('input.description');
const descriptionP = document.querySelector('p.description');
const descriptionButton = document.querySelector('button.description');
const listUl = listDiv.querySelector('ul');
const addItemInput = document.querySelector('input.addItemInput');
const addItemButton = document.querySelector('button.addItemButton');
const listItems = document.getElementsByTagName('li');
const lis = listUl.children;
const firstListItem = listUl.firstElementChild;
const lastListItem = listUl.lastElementChild; 

function attachListItemButtons (li) {
  let up = document.createElement('button');
  up.className = 'up';
  up.textContent = 'up';
  li.appendChild(up);

  let down = document.createElement('button');
  down.className = 'down';
  down.textContent = 'down';
  li.appendChild(down);

  let remove = document.createElement('button');
  remove.className = 'remove';
  remove.textContent = 'remove';
  li.appendChild(remove);
}


for (let i = 0; i < lis.length; i += 1) {
  attachListItemButtons(lis[i]);
}


//Loop through the list items
//If there are no previousElementSiblings to li: hide the second child node (UP button) of the list item
//Else if there are no nextElementSiblings to li: set style: 'display: none' (for aesthetics**) to the third child node (DOWN button) of the list item
//**Note: hiding the DOWN button causes a gap between the UP button and REMOVE button
//Else do not hide/display:none
function hideUpDownBtns() {
  for (let i = 0; i < lis.length; i++) {
    if (!lis[i].previousElementSibling) {
      lis[i].childNodes[1].style.visibility = 'hidden';
    } else if (!lis[i].nextElementSibling){
      lis[i].childNodes[2].style.display = 'none';
    } else {
      lis[i].childNodes[1].style.visibility = 'visible';
      lis[i].childNodes[2].style.display = 'flex';
    }
  }
}

//Call the function on page load
hideUpDownBtns();


listUl.addEventListener('click', (event) => {
  if (event.target.tagName == 'BUTTON') {
    if (event.target.className == 'remove') {
      let li = event.target.parentNode;
      let ul = li.parentNode;
      ul.removeChild(li);
    }
    if (event.target.className == 'up') {
      let li = event.target.parentNode;
      let prevLi = li.previousElementSibling;
      let ul = li.parentNode;
      if (prevLi) {
        ul.insertBefore(li, prevLi);
      }
    }
    if (event.target.className == 'down') {
      let li =event.target.parentNode;
      let nextLi = li.nextElementSibling;
      let ul = li.parentNode;
      if (nextLi) {
        ul.insertBefore(nextLi, li);
      }
    }
    //Call the function when a button is pressed to refresh button styles
    hideUpDownBtns();
  }
});


toggleList.addEventListener('click', function() {
  if (listDiv.style.display == 'none') {
    toggleList.textContent = 'Hide list';
    listDiv.style.display = 'block';
  } else {
    toggleList.textContent = 'Show list';
    listDiv.style.display = 'none';
  }
});

descriptionButton.addEventListener('click', function() {
  descriptionP.innerHTML = descriptionInput.value + ':';
  descriptionInput.value = '';
});

addItemButton.addEventListener('click', function() {
  let ul = document.getElementsByTagName('ul')[0];
  let li = document.createElement('li');
  li.textContent = addItemInput.value;
  attachListItemButtons(li);
  ul.appendChild(li);
  addItemInput.value = '';
  //Call the function after adding item to refresh button styles
  hideUpDownBtns();
});

Thanks! This worked for me!

Ricardo Gainda
Ricardo Gainda
5,017 Points

Can you explain why childNode[1] works for hiding the Up button? Everywhere I look to for an explanation on childNode says the index starts at 0. When I try to substitute 0 for 1 to see what would be hidden instead I get an error, so I can't quite follow.

Edit: Hey, you know what, I figured it out! I checked this in the console:

lis[1].childNodes

and it returned this:

NodeList(4) [ #text, button.up, button.down, button.remove ]

1 Answer

This was so so helpful, thanks for sharing!

I had a particularly hard time trying to figure out:

  1. How to get the first and last item (got stuck trying to force firstElementChild/lastElementChild) and
  2. style.visibility = 'hidden' compared to style.display = 'none'.

I couldn't have figured it out without you sharing your solution, I was just stuck. Thanks again!