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 DOM Scripting By Example Editing and Filtering Names Moving to an Editing State

Tomasz Cysewski
Tomasz Cysewski
2,736 Points

Input element is not working properly

Hello, I changed button from edit to save and now I want to take value of input and replace this value with the span. It looks like I am confused with multiple inputs. What's the matter? Thx for help

<!DOCTYPE html>
<html>
<head>
  <meta name="viewport" content="width=device-width, initial-scale=1">
  <title>RSVP App</title>
  <link href="https://fonts.googleapis.com/css?family=Courgette" rel="stylesheet">
  <link href="https://fonts.googleapis.com/css?family=Lato:400,700" rel="stylesheet">
  <link href="css/style.css" rel="stylesheet">
</head>
<body>
  <div class="wrapper">
    <header>
      <h1>RSVP</h1>
      <p>A Treehouse App</p>
      <form id="registrar">
        <input type="text" name="name" placeholder="Invite Someone">
        <button type="submit" name="submit" value="submit">Submit</button>
      </form>
    </header>

    <div class="main">  
      <h2>Invitees</h2>
      <ul id="invitedList"></ul>    
    </div>
  </div>
  <script src="app.js"></script>
</body>
</html>
const form = document.getElementById('registrar');
const input = form.querySelector('input');
const ul = document.getElementById('invitedList');

function createli(text){
  const li = document.createElement('li');

  const span = document.createElement('span');
  span.textContent = text;
  li.appendChild(span);

  const label = document.createElement('label');
  label.textContent = 'Confirmed';
  li.appendChild(label);

  const checkbox = document.createElement('input');
  checkbox.type = 'checkbox';
  label.appendChild(checkbox);


  const editbutton = document.createElement('button');
  editbutton.textContent = 'Remove';

  const removebutton = document.createElement('button');
  removebutton.textContent = 'Edit';

  li.appendChild(editbutton); 
  li.appendChild(removebutton); 
  return li;
}

form.addEventListener('submit', (e) =>{
  e.preventDefault();
  const text = input.value;
  input.value = '';
  const li = createli(text);
  ul.appendChild(li);


});

ul.addEventListener('change', (e) => {
  const checkbox = e.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.tagName === 'BUTTON'){
      const button = e.target;
      const li = e.target.parentNode;
      const ul = li.parentNode;
      const span = li.firstElementChild;
      const createinput = document.createElement('input');
      createinput.className='second';
      const createspan = document.createElement('span');

      if(button.textContent === 'Remove'){
      ul.removeChild(li);
      }
      else if(button.textContent === 'Edit'){
        input.type = 'text';
        input.value = span.textContent;
        li.insertBefore(createinput, span);
        li.removeChild(span);
        button.textContent = 'Save';
      }
      else if(button.textContent === 'Save'){
//        take input.value and change it with span
          console.log('save');  
          const input = li.querySelector('input.second');

          li.insertBefore(createspan, input);
          createspan.textContent = input.value;
          li.removeChild(input);


          button.textContent = 'Edit';
//        rename the button to edit and switch input to span

      }

  }

});

1 Answer

Steven Parker
Steven Parker
229,657 Points

Remember that input is a global that represents the main input box.

But for your "edit" button, you don't want to use that one, but instead you want to use the one that is about to become part of the same list item. So in the handler for the "edit" button, where the first few lines reference input they should reference createInput instead.