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 Improving the Application Code Refactor 1: Create List Items

Leonard Morrison
PLUS
Leonard Morrison
Courses Plus Student 32,914 Points

I keep getting errors on the double createLI method. Where exactly did I go wrong?

I keep getting errors when I try to connect to the label and the checkbox. I don't know where I went wrong. Please help as I don't where I went wrong. I tried everything. Thanks in advance.

https://w.trhou.se/l5wqzp53fv

document.addEventListener("DOMContentLoaded", () =>
{
  const reg = document.getElementById("registrar");
  const invited = document.getElementById("invitedList");
  const input = reg.querySelector("input");
  const div = document.createElement("div");

  //Javascript only code
  const filterLabel = document.createElement("label");
  const filterCheckBox = document.createElement("input");
  const mainDiv = document.querySelector(".main");
  filterLabel.textContent = "Hide those who haven't responsed";
  filterCheckBox.type = "checkbox";
  div.appendChild(filterLabel);
  div.appendChild(filterCheckBox);
  mainDiv.insertBefore(div, invited);

  filterCheckBox.addEventListener("change", (e) =>  
                                  {
                                    const isChecked = e.target.checked;
                                    const lilist = invited.children;

                                    if(isChecked)
                                    {
                                      for(let i =0; i < lilist.length; i += 1)
                                      {
                                       let li = lilist[i];
                                        if(li.className === "responded")
                                        {
                                          li.style.display = '';
                                        }
                                        else
                                        {
                                          li.style.display = 'none';
                                        }
                                      }
                                    }
                                    else
                                    {
                                      for(let i =0; i < lilist.length; i += 1)
                                      {
                                       let li = lilist[i];
                                       li.style.display = '';
                                      }

                                    }



                                  });

  function createLI(text)
  {
    function createElement(elementName, property, value)
    {
     const element = document.createElement(elementName);
     element[property] = value;
     return element; 
    }

    function appendToLI(elementName, property, value)
    {
      const element = createElement(elementName, property, value);
      li.appendChild(element);

    }




    const li = document.createElement("li");
    appendToLI("span", "textContent", text);
    appendToLI("label", "textContent", "Confirmed").appendChild(createElement("input", "type", "checkbox"));




    //Add an edit button
    appendToLI("button", "textContent", "Edit");



    //Add a delete button
    appendToLI("button", "textContent", "Remove");

    invited.appendChild(li);
    input.value = "";
    return li;

  }


  reg.addEventListener("submit", (e) => 
                       {
                         e.preventDefault();
                         const text = input.value;
                        createLI(text);                     
                       });

  invited.addEventListener("change", (e) => 
                           {
                             const checkbox = event.target;
                             const checked = checkbox.checked;
                             const listItem = checkbox.parentNode.parentNode;

                             if(checked)
                             {
                               listItem.className = 'responded'
                             }

                             else
                             {
                               listItem.className = 'pending';
                             }
                           });

  invited.addEventListener("click", (e) =>
                           {
                            if(e.target.tagName === "BUTTON")
                            {
                              const li = e.target.parentNode;
                              if(e.target.textContent === "Remove")
                              {
                                const ul = li.parentNode;
                                ul.removeChild(li);
                              }

                              else if (e.target.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);
                                e.target.textContent = "Save";
                              }

                              else if(e.target.textContent === "Save")
                              {
                               const input = li.firstElementChild;
                               const span = document.createElement("span");
                               span.textContent = input.value;
                               li.insertBefore(span, input);
                               li.removeChild(input); 
                               e.target.textContent = "Edit";

                              }

                            }


                         });
});

Hi, can you share the error that you are getting?

An error has information that can quickly point to the problem, so if you share an error that you're seeing in the console, that would be the most helpful.

1 Answer

Oriol Jurnet
Oriol Jurnet
9,515 Points

Hi Leban Mohamed , I think I have the answer. I had the same problem as you and finally came to a solution.

Here is where the error is generated, right?: appendToLI("label", "textContent", "Confirmed").appendChild(createElement("input", "type", "checkbox"));

To fix it, you need to "return element;" in your previous function appendToLI(); if not, the result of calling that function is "undefined" (even it still generates the element) and thus the JS compiler can't call the method .appendChild of an undefined.

Hope this helped you!

Kind regards,

Oriol