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

DOM scripting by example Editing names

My code worked up to Adding names, then fell apart with editing and filtering names. I've proofread several times. Can someone please help? This is very frustrating. (I took a snapshot, but I don't see how to add it here so I just copied and pasted.)

const form = document.getElementById('registrar');
const input = form.querySelector('input');

const mainDiv = document.querySelector('.main');
const ul = document.getElementById('invitedList');

const div = document.createElement('div');
const filterLabel = document.createElement('label');
const filterCheckbox = documnet.createElement('input');

filerLabel.textContent = "Hide those who haven't responded";
filterCheckbox.type = 'checkbox';
div.appendChild(filterLabel);
div.appendChild(filterCheckbox);
mainDiv.insertBefore(div, ul);
filterCheckbox.addEventListener('change', (e) => {
  const ifChecked = e.target.checked;
  const lis = ul.children;
  if (isChecked) {
    for (let i = 0; i < lis.length; i += 1) {
      let li = lis[i];
      if (li.className === 'responded') {
        li.style.display = '';
       } else {
          li.style.display = 'none';
      } 
    } else {
      for (let i = 0; i < lis.length; i += 1) {
        let li = lis[i];     
    } 
  }
});

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.removeElement('button');
  removebutton.textContent = 'Remove';
   li.appendChild(removeButton); 
  return li;
}

form.addEventListener('Submit', (e) =>{
  e.preventDefault();
  const text = input.value;
  input.value = '';
  const li = createli(text);
  ul.appendChild(li);


});

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

ul.addEventListener('click', (e) => {
  if(e.target.tagName === 'BUTTON'){
      const button = e.target;
      const li = e.target.parentNode;
      const ul = li.parentNode;

      if(button.textContent === 'Remove'){
      ul.removeChild(li);
       } else if  (button.textContent = 'Edit') {
         const span = li.firstElementChild;
         const input = document.createElement('input');
         input type = 'text';
         input.value = span.textContext;
         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.textContext = input.value;
        li.insertBefore(span, input);
        li.removeChild(input);
        button.textContent = 'Edit';
      }

  }

});
Steven Parker
Steven Parker
229,981 Points

When you make a workspace snapshot, you post the link (the URL) to it here. It would also be helpful to provide a link to the course page you are working with, and describe the behavior you call "fell apart" in a bit more detail.