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 Perfect

How do I change the text of the 'Edit' button when I click on it?

I would like it to say "Done" when I go into edit mode and say "Edit" when it's not in edit mode.

Here is a snapshot of all of my code: https://w.trhou.se/8w7i1rnbmc

Marcel Schmidt
Marcel Schmidt
12,134 Points

Hi there Zack,

I was just wrappin my head around this too and found a solution. I don't know if this is the intended/proper way to do it, but this is how I got it to work. I marked the code with comment arrows:

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
    listItem.querySelector("button.edit").innerText = "Edit"; // <--------------
    label.innerText = editInput.value;
  } else {
      // Switch to .editMode
      // input's value becomes the label's text
    listItem.querySelector("button.edit").innerText = "Save"; // <-------------
    editInput.value = label.innerText;
  }

  // Toggle .editMode on the list item
  listItem.classList.toggle("editMode");
}

Hope this helps, if you don't understand wh/how this is working, feel free to ask.