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>
  <meta name="viewport" content="width=device-width, initial-scale=1">
  <title>RSVP App</title>
  <link href="" rel="stylesheet">
  <link href=",700" rel="stylesheet">
  <link href="css/style.css" rel="stylesheet">
  <div class="wrapper">
      <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>

    <div class="main">  
      <ul id="invitedList"></ul>    
  <script src="app.js"></script>
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;

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

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

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

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

  return li;

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


ul.addEventListener('change', (e) => {
  const checkbox =;
  const checked = checkbox.checked;
  const ListItem = checkbox.parentNode.parentNode;
      ListItem.className = 'responded';
      ListItem.className = '';

ul.addEventListener('click', (e) => {
  if( === 'BUTTON'){
      const button =;
      const li =;
      const ul = li.parentNode;
      const span = li.firstElementChild;
      const createinput = document.createElement('input');
      const createspan = document.createElement('span');

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

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

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




1 Answer

Steven Parker
Steven Parker
207,999 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.