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
200,417 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.