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

How to remove 'up' in the first li and 'down' in last

I wrote this code, but is there any ways to remove 'up' for the each element that standing on the top? The same for the last one

Code:

const listUl = list.querySelector('ul');

function removeMessButtons(ul) { let firstChild = ul.firstElementChild; firstChild.removeChild( firstChild.getElementsByClassName('up')[0] );

let lastChild = ul.lastElementChild; lastChild.removeChild( lastChild.getElementsByClassName('down')[0] ); }

removeMessButtons( listUl );

Maxim Melnic
Maxim Melnic
4,176 Points

Между нами) Судя по твоим поинтам ты далеко ушел в изучении js на этой платформе. Скажи мне, как тебе в целом этот курс?

1 Answer

Maxim Melnic
Maxim Melnic
4,176 Points

Hi, see my solution:

function hidButtons ()  {
  let ul = document.getElementsByTagName("ul")[0];
  for (let i = 0; i < ul.children.length; i ++) {
  ul.children[i].firstElementChild.style.opacity = "1";
  ul.children[i].lastElementChild.style.opacity = "1";
  }
  ul.firstElementChild.firstElementChild.style.opacity = "0";
  ul.lastElementChild.lastElementChild.style.opacity = "0";
} 
document.addEventListener("click", (event) => {
     hidButtons (); 
 })

hidButtons ();

Note: In my code up button is the first element in <li>, and down button is the last element. Remove button is in center.