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

Changing Input Value

I'm trying to change the inputText value and/or set attribute to value but it doesn't seem to be working, any thought?

// Problem: User interaction doesn't provide desired results.
// Solution: Add interactivity so the user can manage daily tasks.

let taskInput = document.getElementById('new-task');//new-task
let addButton = document.getElementsByTagName('button')[0]; //first button
let incompleteTaskHolder = document.getElementById('incomplete-tasks');
let completedTaskHolder = document.getElementById('completed-tasks');
let paraOne = document.getElementsByTagName("p");

//Add a new task


let createNewTaskElement = function(taskstring){

let newTask = document.createElement("li");  
let checkBox = document.createElement("input");
let label = document.createElement("label");
let inputText = document.createElement("input");
let editButton = document.createElement("button");
let deleteButton = document.createElement("button");

  checkBox.type = "checkbox";
  inputText.type = "text";
  inputText.setAttribute = ("value", "value");
  editButton.innerText = "Edit";
  editButton.className = "edit";
  deleteButton.innerText = "Delete";
  deleteButton.className = "delete";
  //When the button is pressed
  //Create a new list item with the text from #new-task:

  label.innerText = taskstring;


  newTask.appendChild(checkBox);
  newTask.appendChild(label);
  newTask.appendChild(inputText);
  newTask.appendChild(editButton);
  newTask.appendChild(deleteButton);




  return newTask;

  }

  let reLabel = function (){  
    var paraOne = this.childNodes;



  }  

let addTask = function() {
  let moreTask = createNewTaskElement(taskInput.value);
  incompleteTaskHolder.appendChild(moreTask);
  bindTaskEvents(moreTask, taskCompleted);
  //input (Checkbox)
  taskInput.value = "";
    //label
    //input (texts)
    //button.edit
    //button.delete
    //Each elements, need modified and appended

}

 //Edit an existing task
let editTask = function() {
  console.log("edit Task");
    //When the Edit button is pressed
      //if the class of the parent is .editMode
        //Switch from .editMode
        //label texst become the input's value
  var newTask = this.parentNode;
  var ul = newTask.parentNode;
  var textInput = newTask.querySelector("input[type=text");
  var label = newTask.querySelector("label");


var containsClass = newTask.classList.contains("editMode");

if(containsClass) {

    label.innerText = textInput.value;



}
  else {

 textInput.value = label.innerText;

  }

  newTask.classList.toggle("editMode");

}

  //Delete an existing task
    let deleteTask = function() {
      console.log("box is checked");
       //When the Delete button is pressed
          //Remove the parent list item from the url
        var newTask = this.parentNode;
        var ul = newTask.parentNode;

  //Remove the parent list item from the ul
        ul.removeChild(listItem);
    }


   let taskIncomplete = function() {
   console.log("Task incomplete..");
     let newTask = this.parentNode;
      incompleteTaskHolder.appendChild(newTask);
     bindTaskEvents(newTask,taskCompleted);
}



 //Mark a task as complete
    let taskCompleted = function() {
     //When the checkbox is unchecked
      //Append the task list item to the #incomplete-task
      let newTask = this.parentNode
      completedTaskHolder.appendChild(newTask);
       bindTaskEvents(newTask,taskIncomplete);
    }

let bindTaskEvents = function(taskListItem, checkBoxEventHandler){
  console.log("bind list item events");
//Select taskListItem's children
    let checkBox = taskListItem.querySelector("input[type=checkbox]");
    let editButton = taskListItem.querySelector("button.edit");
    let deleteButton = taskListItem.querySelector("button.delete");
  //bind editTask to edit button
    editButton.onclick= editTask;
  //bind deleteTask to delete button
    deleteButton.onclick = deleteTask;
  //bind checkBoxEventHandler to checkbox
    checkBox.onchange = checkBoxEventHandler;

}
//Set the click handler to teh addTask funciton
addButton.onclick = addTask;

//Cycle over the incompleteTaskHolder ul list item
for(let i = 0; i < incompleteTaskHolder.children.length; i++) {

  bindTaskEvents(incompleteTaskHolder.children[i],taskCompleted);

}
    //bind events to the list items children (taskCompleted)

for(let i = 0; i < completedTaskHolder.children.length; i++) {
  bindTaskEvents(completedTaskHolder.children[i], taskIncomplete);

}

1 Answer

Hi Michael, you're calling the setAttribute method incorrectly. The line should read something like:

inputText.setAttribute('value', 'strawberry');