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

Jacek Skrzypek
Jacek Skrzypek
5,040 Points

Need help in removing and attaching buttons after every click

I tried to avoid attaching 'up' button to first li and 'down' to the last by putting an if statement into the attachListItemButtons(li) and then removing and attaching buttons after each click:

function attachListItemButtons(li) {
  if (li != firstListItem) {
  let up = document.createElement('button');
  up.className = 'up';
  up.textContent = 'Up';
  li.appendChild(up);
  };
  if (li != lastListItem) {
  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 ++) {
  attachListItemButtons(lis[i]);
}

function removeListItemButtons(li) {
  while (li.firstElementChild) {
    let button = li.firstElementChild;
    li.removeChild(button);
  }
};

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);
      }
    }
    for (let i = 0; i < lis.length; i ++) {
      removeListItemButtons(lis[i]);
    }
    for (let i = 0; i < lis.length; i ++) {
      attachListItemButtons(lis[i]);
    }
  }
});

But the if statements don't seem to work 'inside' the click, as if inserting would not affect the first(and last)ListItem property?

I guess hiding the buttons would make more sense than removing ant attaching them, but I still would like to know, why my first solution didn't work.

2 Answers

KRIS NIKOLAISEN
KRIS NIKOLAISEN
54,372 Points

firstListItem and lastListItem refer to the first and last list items respectively at the time of assignment: in this case when the page loads. By declaring them at the top of the script firstListItem will always be the 'grapes' list item and lastListItem will always be the 'plums' list item regardless of where they are moved up and down the list.

To get the current first and last list items, they should be read inside the attachListItemButtons() function.

function attachListItemButtons(li) {
    if (li != listUl.firstElementChild) {
        let up = document.createElement('button');
        up.className = 'up';
        up.textContent = 'Up';
        li.appendChild(up);
    };

    if (li != listUl.lastElementChild) {
        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);
}