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 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="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">
  <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 = e.target;
  const checked = checkbox.checked;
  const ListItem = checkbox.parentNode.parentNode;
      ListItem.className = 'responded';
      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');
      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
216,057 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.