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

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 );

ΠœΠ΅ΠΆΠ΄Ρƒ Π½Π°ΠΌΠΈ) Будя ΠΏΠΎ Ρ‚Π²ΠΎΠΈΠΌ ΠΏΠΎΠΈΠ½Ρ‚Π°ΠΌ Ρ‚Ρ‹ Π΄Π°Π»Π΅ΠΊΠΎ ΡƒΡˆΠ΅Π» Π² ΠΈΠ·ΡƒΡ‡Π΅Π½ΠΈΠΈ js Π½Π° этой ΠΏΠ»Π°Ρ‚Ρ„ΠΎΡ€ΠΌΠ΅. Π‘ΠΊΠ°ΠΆΠΈ ΠΌΠ½Π΅, ΠΊΠ°ΠΊ Ρ‚Π΅Π±Π΅ Π² Ρ†Π΅Π»ΠΎΠΌ этот курс?

1 Answer

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.