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

Erik Biebinger
Erik Biebinger
7,299 Points

When I hover over a list item, it makes them uppercase but the recently added remove button disappears. Why is that?

When I hover over a list item, it makes them uppercase but also lets the remove-button disappears.

Why is that?

I think it's because taskList.addEventListener('mouseover', (event) => { if(event.target.tagName === 'LI'){ event.target.textContent = event.target.textContent.toUpperCase(); } }) adds also a "textContent" to the button? Eventho its not really added text.

Hmm...

Thanks in advance!

This is my code:

const btnCreate = document.getElementById('btn-main');
const btnToggle = document.querySelector('.btn-toggle');
const btnRemove = document.querySelector('.btn-remove');
const taskList = document.querySelector('.list-container ul');
const listItems = taskList.children;

function attachRemoveButton(li){
  let remove = document.createElement('button')
  remove.className = 'remove';
  remove.textContent = 'Remove';
  li.appendChild(remove)
}
for (let i=0; i< listItems.length; i++){
  attachRemoveButton(listItems[i]);
};

taskList.addEventListener('click', (event)=>{
  if(event.target.tagName === 'BUTTON'){
    const button = event.target;
    const li = button.parentNode;
    li.remove()
  }
})

btnCreate.addEventListener('click', () => {
  let ul = document.getElementsByTagName('ul')[0];
  const input = document.querySelector('.input-main');
  let li = document.createElement('li');
  li.textContent = input.value;
  attachRemoveButton(li);
  ul.appendChild(li);
  input.value = '';
});


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


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

btnToggle.addEventListener('click', () => {
  const listContainer = document.querySelector('.list-container');
  if (listContainer.style.display === 'none') { 
    btnToggle.textContent = 'Hide List';
    listContainer.removeAttribute('style');
  } else {
    btnToggle.textContent = 'Show List';                        
    listContainer.style.display = 'none';
  }                         
});

btnCreate.addEventListener('click', () => {
  const input = document.querySelector('.input-main');
  const list = document.querySelector('ul');
  list.insertAdjacentHTML( 
    'afterbegin', 
    `<li>${input.value}</i>` 
  );
  input.value = '';
});

1 Answer

Steven Parker
Steven Parker
229,744 Points

The MDN page for textContent contains this caveat:

Warning: Setting textContent on a node removes all of the node's children and replaces them with a single text node with the given string value.

What you probably intended was to just replace the content of the text node which is the first child of the list item:

    event.target.childNodes[0].textContent = event.target.childNodes[0].textContent.toUpperCase();