Welcome to the Treehouse Community

The Treehouse Community is a meeting place for developers, designers, and programmers of all backgrounds and skill levels to get support. Collaborate here on code errors or bugs that you need feedback on, or asking for an extra set of eyes on your latest project. Join thousands of Treehouse students and alumni in the community today. (Note: Only Treehouse students can comment or ask questions, but non-students are welcome to browse our conversations.)

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and a supportive community. Start your free trial today.

JavaScript DOM Scripting By Example Editing and Filtering Names Filter Invitees Who Have Not Responded

Why does my code not work?

I get the syntax error "Cannot read property 'className' of undefined[.]" Can you please explain what is not working?

Here is mycode:

const form = document.querySelector('#registrar');
const input = form.querySelector('input');
const ul = document.querySelector('#invitedList');

const main = document.querySelector('.main');
const div = document.createElement('div');
const label = document.createElement('label');
const filter = document.createElement('input');

main.insertBefore(div, ul);
div.appendChild(label);
div.appendChild(filter);
filter.type = 'checkbox';
label.textContent = 'Show Selected Items';

div.addEventListener('change', function filter(e) {
 const unchecked = ul.children;
 if(e.target.tagName === 'INPUT') {
  for(let i = 0; i < ul.children.length; i += 1) {
    let li = ul[i];
    console.log(ul.children);
   if (li.className =='responded') {
    li.style.display = '';      
  } else {
    li.style.display = 'none';
  }
  }
 }
});
function createLI(text) {
  const li = document.createElement('li');
  const span = document.createElement('span');
  span.textContent = text;
  li.appendChild(span);
  const label = document.createElement('label');
  label.textContent ='Confirmed';
  const checkbox = document.createElement('input');
  checkbox.type = 'checkbox';  
  label.appendChild(checkbox);
  li.appendChild(label);

  const editButton = document.createElement('button');
  editButton.textContent = 'Edit';
  li.appendChild(editButton);

  const removeButton = document.createElement('button');
  removeButton.textContent = 'Remove';
  li.appendChild(removeButton);

  return li;
}


form.addEventListener('submit', function submit(name) {
  name.preventDefault();
  const text = input.value;
  input.value = '';
  const li = createLI(text);
  ul.appendChild(li);
});

ul.addEventListener('change', function styling(e) {
  const checkbox = e.target;
  const checked = e.target.checked;
  const listItem = checkbox.parentNode.parentNode;
  if (checked) {
    listItem.className = 'responded';
  } else {
    listItem.className = '';
  }
});

ul.addEventListener('click', function remove(e) {
const listItem = e.target.parentNode;
const ul = listItem.parentNode;
const button = e.target;
const li = button.parentNode;
 if (button.tagName == 'BUTTON') {
     if (button.textContent === 'Remove') {
      ul.removeChild(listItem);
   } else if (button.textContent === 'Edit') {
     const span = li.firstElementChild;
     const input = document.createElement('input');
     input.type = 'text';
     input.value = span.textContent;
     li.insertBefore(input, span);
     li.removeChild(span);
     button.textContent = 'Save';
   } else if (button.textContent === 'Save') {
     const input = li.firstElementChild;
     const span = document.createElement('span');
     span.textContent = input.value; 
     li.insertBefore(span, input);
     li.removeChild(input);
     button.textContent = 'Edit';
   }
 }
});

2 Answers

can u give a snapshot?

What do you mean by snapshot?

Aleksandra Frankowska
Aleksandra Frankowska
8,936 Points

On line 20 it should be: let li = ul.children[i];