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 All Children of a Node with children

A R
A R
12,834 Points

Can't figure out the problem with the FOR loop...

I can't figure out why the for loop isn't working. I'm getting an 'i is undefined error'

const toggleList = document.getElementById('toggleList');
const listDiv = document.querySelector('.list');
const list = listDiv.querySelector('ul')
const input = document.querySelector('input.description');
const descP = document.querySelector('p.description');
const descriptionButt = document.querySelector('button.description');
const addItemIn = document.querySelector('input.addItemIn');
const addButt = document.querySelector('button.addButt');
const removeButt = document.querySelector('button.removeButt');
const listItem = document.getElementsByTagName('li');
const existing = list.children;

function addButts (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 del = document.createElement('button');
  del.className = "del";
  del.textContent = "delete";
  li.appendChild(del);

};

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


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

descriptionButt.addEventListener('click', () => {
  descP.innerHTML = input.value + ':';
});

addButt.addEventListener('click', () => {
  let ul = document.getElementsByTagName('ul')[0];
  let li = document.createElement('li');
  li.textContent = addItemIn.value;
  ul.appendChild(li);
  addButts(li);
  addItemIn.value = '';
                           });

list.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 ul = li.parentNode;
   let prevLi = li.previousElementSibling;
     if (prevLi)
     {ul.insertBefore(li, prevLi);}
   }
        if (event.target.className === 'down'){
   let li = event.target.parentNode;
   let ul = li.parentNode;
   let nextLi = li.nextElementSibling;
     ul.insertBefore(nextLi, li);
      }
    }
  }
  );

2 Answers

Steven Parker
Steven Parker
231,248 Points

There's a stray semicolon between the "for" loop and the brace that starts the loop body.

And for future questions, a better way to show workspace code that has multiple components (HTML and/or CSS along with JavaScript) is to make a snapshot of your workspace and post the link to it here.

A R
A R
12,834 Points

Ah, thank you