Welcome to the Treehouse Community

The Treehouse Community is a meeting place for developers, designers, and programmers of all backgrounds and skill levels to get support. Collaborate here on code errors or bugs that you need feedback on, or asking for an extra set of eyes on your latest project. Join thousands of Treehouse students and alumni in the community today. (Note: Only Treehouse students can comment or ask questions, but non-students are welcome to browse our conversations.)

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and a supportive community. Start your free trial today.

JavaScript Interactive Web Pages with JavaScript Traversing and Manipulating the DOM with JavaScript Perform: Changing Classes

What's the point of the "else" clause in the "editTask" function?

If I remove the "else" clause it seems the apps functions with no problems. Why is it included?

var editTask = function() {
  console.log("Edit task...");
  var listItem = this.parentNode;

  var editInput = listItem.querySelector("input[type=text]");
  var label = listItem.querySelector("label");

  var containsClass = listItem.classList.contains("editMode");
  //if the class of the parent is .editMode
  if (containsClass) {
    //Switch from .editMode
    //label text become the input's value
    label.innerText = editInput.value;
  } else {
    //Switch to .editMode
    //input value becomes the label's text
    editInput.value = label.innerText;
  }
  //Toggle .editMode on the listItem
  listItem.classList.toggle("editMode");

}

2 Answers

I was having a hard time seeing how these things correlate with each other.

I replaced

editInput.value = label.innerText;

with

 editInput.value = "What's the point of this?";

And can see the correlation. Thanks for the input!

Devin Scheu
Devin Scheu
66,191 Points

The else clause does what it says, if the if statement is not true then the code in the else clause will execute, sometimes its need and sometimes its just there in case there is an error. This is checking if whatever the parent element is, is in edit mode.