JavaScript DOM Scripting By Example Improving the Application Code The DOMContentLoaded Event

"DOMContentLoaded" not working for me.

I have tried refreshing my workspace but still nothing works.

document.addEventListener('DOMContentLoaded ', () => {
  const form = document.getElementById('registrar');
  const input = form.querySelector('input');

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

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

  filterLabel.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 isChecked = 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];
          li.style.display = '';
        }
    }
  });


  function createLI(text) {
     //const ul = document.getElementById('invitedList');
    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', (e) => {
    e.preventDefault();
   // console.log(input.value);
    const text = input.value;
    input.value = '';
    //const ul moved to top of file so it can be globally accessible.
   const li = createLI(text);
    ul.appendChild(li);
  });

  //true(checked) and false(unchecked)
  ul.addEventListener('change', (e) => {
    //console.log(e.target.checked);
    const checkbox = event.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 = button.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.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';
      }
  }
  });
}); ```
Steven Parker
Steven Parker
171,291 Points

This script relies on an HTML component to be able to be tested, so that part of the code should also be shown. Even better, make a snapshot of your workspace and post the link to it here.

1 Answer

Steven Parker
Steven Parker
171,291 Points

On the very top line, there's a space in the string with the event name 'DOMContentLoaded ', it should be 'DOMContentLoaded' (without a space) instead.

Thank you for your eagle eyes!

Steven Parker
Steven Parker
171,291 Points

Well, I didn't spot it just looking, I ran the snapshot. :wink: