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

Where have I gone wrong?

Here's part of the code from my app.js file...

 js
html  form.addEventListener('submit', (e) => {
  e.preventDefault();
  const text = input.value;
  input.value = '';
  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.textContent = text;
  ul.appendChild(li);
 li.appendChild(label);
      li.appendChild(span);

const editbutton = document.createElement("button");
editbutton.textContent = 'edit';
li.appendChild(editbutton);
 const removeButton = document.createElement("button");
 removeButton.textContent = 'remove';
 li.appendChild(removeButton);

});

ul.addEventListener('change', (e) =>{
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.textContent==='remove'){
    const li = e.target.parentNode;
    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);
      button.textContent='save';
  }
});
Steven Parker
Steven Parker
243,318 Points

Since you're sharing only part of the code, I'd guess you have a compelling reason to suspect the problem is in this part. Please explain the behavior of the code that led you to isolate this section.

And sharing the entire project might still be useful to facilitate observing the issue firsthand. If you're using a workspace, you can make a snapshot of your workspace and post the link to it here.

Also, when quoting code for markdown, the triple backticks must be on lines by themselves (or with the language name for syntax coloring). And for proper coloring for this script, use "js" instead of "html".

1 Answer

Thanks Stephen Parker , I've formatted the entirety of my post to hopefully make it clearer to read.

Steven Parker
Steven Parker
243,318 Points

But what's the nature of the issue (what's "wrong")?

And that "snapshot" would help a lot by providing the rest of the code and an environment for a complete analysis.