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 Using previousElementSibling and insertBefore

Madeline Yao
seal-mask
.a{fill-rule:evenodd;}techdegree
Madeline Yao
Full Stack JavaScript Techdegree Student 9,611 Points

The Button In My Code Does Not Work

Here is my code for the tutorial: a. 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>

<button id="hide">Hide list</button>
<div class="list">
    <p>Things that are purple:</p>
    <input type="text" class="blanks">
    <button class="description">Change list description</button>
    <ul>
      <li>grapes    
          <button class= "remove">Remove</button>
          <button class="up">Up</button>
          <button class= "down">Down</button>
      </li>
      <li>amethyst  
          <button class="remove">Remove</button>
          <button class="up">Up </button>
          <button class="down">Down</button>
      </li>
      <li>lavender  
          <button class="remove">Remove</button>
          <button class="up">Up</button>
          <button class="down">Down</button>
      </li>
      <li>plums     
          <button class="remove">Remove</button>
          <button class="up">Up</button>
          <button class="down">Down</button>
      </li>
    </ul>
    <input type="text" class="addItemInput">
    <button class="addItemInput">Add Item</button>

 </div>
<script src="app.js"></script>
<script src="temp.js"></script>

</body> </html> b. app.js: onst p = document.querySelector('p'); const input = document.querySelector('input.blanks'); const button = document.querySelector('button.description'); const list = document.querySelector('.list'); const listUl = list.querySelector('ul'); var hide_button = document.querySelector('#hide'); const add_button = document.querySelector('button.addItemInput'); const add_input = document.querySelector('input.addItemInput'); const lis = listUl.children; const listItems = document.getElementsByTagName('li'); const firstListItem = listUl.firstChildElement; const lastListItem = listUl.lastChildElement; firstListItem.style.backgroundColor = 'blue'; lastListItem.style.backgroundColor = 'red';

button.addEventListener('click', ()=>{ p.value = input.value; });

function addButtonsToNewItems(li){ let up = document.createElement('button'); up.className = 'up'; up.textContent = 'Up'; let down = document.createElement('button'); down.className = 'down'; down.textContent = 'Down';

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

}

for(let i = 0; i < lis.length; i +=1){ addButtonsToNewItems(lis[i]); }

list.addEventListener('mouseover', (event)=>{
  if(event.target.tagName === 'LI'){
    event.target.textContent = event.target.textContent.toUpperCase();                         
  }                       
  }); 

list.addEventListener('mouseout', (event)=>{
  if(event.target.tagName === 'LI'){
    event.target.textContent = event.target.textContent.toLowerCase();                          
  }                         
                            }); 

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


 });

button.addEventListener('click', ()=>{ p.innerHTML = input.value; });

hide_button.addEventListener('click', ()=>{ if(list.style.display === 'none'){ hide_button.textContent = 'hide list'; list.style.display ==='block'; } else{ hide_button.textContent = 'show list'; list.style.display === 'none'; } });

add_button.addEventListener('click', ()=>{ let ul = document.getElementsByTagName('ul')[0]; let li = document.createElement('li'); li.textContent = add_input.value; addButtonsToNewItems(li); ul.appendChild(li); add_input.value = '';

});

I tried to run the code above but I cannot find what makes the button on the webpage not work well. Could anyone please help me find out the bug? Thank you!

Colton Ehrman
Colton Ehrman
255 Points

Do you mind fixing up the code format? It's hard to read.