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 JavaScript and the DOM (Retiring) Traversing the DOM Getting All Children of a Node with children

Dorota Parzych
Dorota Parzych
5,706 Points

something in my code is not working

app.js

const toggleList = document.getElementById('toggleList');
const listDiv = document.querySelector('.list');
const input = document.querySelector('input.description');
const p = document.querySelector('p.description');
const button = document.querySelector('button.description');
const addItemInput = document.querySelector('input.addItemInput');
const addItemButton = document.querySelector('button.addItemButton');
const removeItemButton = document.querySelector('button.removeItemButton');
const listUl = listDiv.querySelector('ul');
const lis = listUl.children;


function attatchListItemButtons (li){
  let up = document.createElement('button');
  up.className = 'up';
  up.textContent = 'up';
  li.appendChild(up);

  let down = document.createElement('button');
  down.className = 'down';
  down.textContent = 'down';
  li.appendChild(down);

  let remove = document.createElement('button');
  remove.className = 'remove';
  remove.textContent = 'remove';
  li.appendChild(remove);

}

for(let i =0; i<lis.length; i++){}

listUl.addEventListener('click', (event) => {
  if(event.target.tagName == 'BUTTON'){
    if(event.target.className == 'none'){
      let li = event.target.parentNode;
      let ul = li.parentNode;
      ul.removeChild(li);
    }

    if(event.target.className == 'up'){
      let li = event.target.parentNode;
      let prevLi = li.previousElementSibling;
      let ul = li.parentNode;
      if(prevLi){
      ul.insertBefore(li, prevLi);
      }


    } 

    if(event.target.className == 'down'){
       let li = event.target.parentNode;
      let nextLi = li.nextElementSibling;
      let ul = li.parentNode;
      if(prevLi){
      ul.insertBefore(nextLi, li );
      }

    }


  }

})

//listDiv.addEventListener('mouseover', (event) => {
//                           if (event.target.tagName == 'LI'){
//                            event.target.textContent = event.target.textContent.toUpperCase();
//                           
//                           }
//                          
//                           })
//                           
//listDivs.addEventListener('mouseout', (event) => {
//  if (event.target.tagName == 'LI'){
//                           event.target.textContent = event.target.textContent.toLowerCase();
//  }
//                           
//                           })         

//document.addEventListener('click',(event) => {
//  console.log(event.target);
//
//});

toggleList.addEventListener('click',() => {
                       if(listDiv.style.display == 'none'){
  listDiv.style.display = 'block';
  toggleList.textContent = 'Hide List'
}else {
  listDiv.style.display = 'none';
  toggleList.textContent = 'Show List'
}


                        });

button.addEventListener('click',() => {
                        p.textContent = input.value + ':';
                        descriptionInput.value = '';

                        })

addItemButton.addEventListener('click', () => {
                               let ul = document.getElementsByTagName('ul')[0];
                               let li = document.createElement('li'); 
li.textContent = addItemInput.value;
attatchListItemButtons(li);
ul.appendChild(li);
addItemInput.value = '';

                               })

removeItemButton.addEventListener('click', () => {
                               let ul = document.getElementsByTagName('ul')[0];
                               let li = document.querySelector('li:last-child'); 
li.textContent = addItemInput.value;
ul.removeChild(li);

                               })   

index.html

<!DOCTYPE html>
<html>
  <head>
    <title>JavaScript and the DOM</title>
    <link rel="stylesheet" href="css/style.css">
  </head>
  <body>
    <h1 id="myHeading">JavaScript and the DOM</h1>
    <p>Making a web page interactive</p>    
    <button id="toggleList">Hide List</button>
    <div class="list">
        <p class="description">Things that are purple:</p>

        <input type="text" class="description">
        <button class="description">Change list description</button>

        <ul>
          <li>grapes 
          </li>
          <li>amethyst

          </li>
          <li>lavender

          </li>
          <li>plums

          </li>
        </ul>
      <input type="text" class="addItemInput">
        <button class="addItemInput">Add item</button>
        <button class ="removeItemButton">Remove last item</button>
      </div>
    <script src="app.js"></script>
<!--    <script src="temp.js"></script>-->
  </body>
</html>

1 Answer

Steven Parker
Steven Parker
231,271 Points

Looks like you have some debugging to do. Here's a couple items to start with:

  • line 7 selects 'button.addItemButton', but there isn't any element in the HTML that meets that spec
  • line 101 references "descriptionInput.value", but no "descriptionInput" has been defined

Your browser dev tools can be very helpful in finding these issues. If you're not familiar with them, you might want to take the Debugging JavaScript in the Browser workshop.