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 Interactive Web Pages with JavaScript Traversing and Manipulating the DOM with JavaScript Perform: Modifying Elements

Robin Malhotra
Robin Malhotra
14,883 Points

Edit Button not being created

It appears my buttons aren't being created at all. Running breakpoints, it seems that

var deleteButton=taskListItems.querySelector("button.delete"); 

is returning null. Any idea how to fix this? My code is below

var newTask=document.getElementById("new-task");
var addTaskButton=document.getElementsByTagName("button")[0];
var incompleteTaskHolder=document.getElementById("incomplete-tasks");
var completedTaskHolder=document.getElementById("completed-tasks");

var createNewTaskElement=function(taskString)
{
    var listItem=document.createElement("li");
    var checkBoxInput=document.createElement("input");
    var label=document.createElement("label");
    var editInput=document.createElement("input");
    var editButton=document.createElement("button");
    var deleteButton=document.createElement("button");



    checkBoxInput.type="checkbox";
    editInput.type="text";  

    if (typeof editButton.innerText === "undefined")  {
        editButton.textContent = "Edit";
      } else {
        editButton.innerText = "Edit";
      }
    editButton.className="edit";
    editButton.className="delete";
    if (typeof deleteButton.innerText === "undefined")  {
        deleteButton.textContent = "Delete";
      } else {
        deleteButton.innerText = "Delete";
      }
    listItem.appendChild(checkBoxInput);
    listItem.appendChild(label);
    listItem.appendChild(editInput);
    listItem.appendChild(editButton);
    listItem.appendChild(deleteButton);

    return listItem;
}

var addTask=function()
{
  console.log("addTaskTriggered");
  var listItem=createNewTaskElement("Do something");

  incompleteTaskHolder.appendChild(listItem);
    bindTaskEvents(this.parentNode,taskCompleted);

}

var editTask=function()
{
console.log("edit");
}


var deleteTask=function()
{
console.log("delete");
this.parentNode.remove();
}

var taskCompleted=function()
{
    console.log("taskCompleted");
    bindTaskEvents(this.parentNode,taskIncomplete);
    completedTaskHolder.appendChild(this.parentNode);
}

var taskIncomplete=function()
{
    console.log("taskIncomplete");
    bindTaskEvents(this.parentNode,taskCompleted);
    incompleteTaskHolder.appendChild(this.parentNode);


}

var bindTaskEvents=function(taskListItems,checkBoxEventHandler)
{
    console.log("adsca");
    var checkBox=taskListItems.querySelector("input[type=checkbox]");
    var editButton=taskListItems.querySelector("button.edit");
    var deleteButton=taskListItems.querySelector("button.delete");
    editButton.onclick=editTask;
    deleteButton.onclick=deleteTask;
    checkBox.onchange=checkBoxEventHandler;
}

addTaskButton.onclick=addTask;
for (var i=0;i<incompleteTaskHolder.children.length;i++)
{
    bindTaskEvents(incompleteTaskHolder.children[i],taskCompleted);
}
for (var i=0;i<completedTaskHolder.children.length;i++)
{
    bindTaskEvents(completedTaskHolder.children[i],taskIncomplete);
}

1 Answer

The bug is in your createNewTaskElement function. Change this code:

    if (typeof editButton.innerText === "undefined")  {
        editButton.textContent = "Edit";
      } else {
        editButton.innerText = "Edit";
      }
    editButton.className="edit";
    editButton.className="delete";
    if (typeof deleteButton.innerText === "undefined")  {
        deleteButton.textContent = "Delete";
      } else {
        deleteButton.innerText = "Delete";
      }

To this:

    if (typeof editButton.innerText === "undefined")  {
        editButton.textContent = "Edit";
      } else {
        editButton.innerText = "Edit";
      }
    editButton.className="edit";

    if (typeof deleteButton.innerText === "undefined")  {
        deleteButton.textContent = "Delete";
      } else {
        deleteButton.innerText = "Delete";
      }
    deleteButton.className="delete";

You were giving the editButton the wrong classname. You overwrote the edit classname with the delete classname. I've corrected and moved the line for the deleteButton so it's clearer.