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

CSS

Nermin Kekic
Nermin Kekic
6,971 Points

textContent property unable to change text on button??

Hi, I cant not figure out why the button does not change text when its clicked. This line is suppose to set the text on button but it does not?

toggleList.style.textContent = 'Hide List'; 
const toggleList = document.getElementById('togglelist');
const listDiv = document.querySelector('.list');
const input = document.querySelector('input');
const p = document.querySelector('p.description');
const button = document.querySelector('button');

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

  }

});

button.addEventListener('click', () => {
  p.innerHTML = input.value + ':';
});
<!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>
  </div>
  <script src="app.js"></script>

</body>

</html>

1 Answer

Steven Parker
Steven Parker
231,261 Points

The "textContent" property is associated directly with the element. It's not a sub-property of "style".

Try this instead:

toggleList.textContent = 'Hide List'; 
Nermin Kekic
Nermin Kekic
6,971 Points

Ahhh ok thank you! I was wondering why my code editor was not automatically displaying that function. Because it can not find it under "style" object. That should have been a give away. Thank you works like a charm!