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) Making Changes to the DOM Styling Elements

unexpected output of toggle button...

Hi,

It would appear the code is written properly, but when the hide list button is pressed it writes a semi-colon to the display style which causes the if statement not to recognize the result.

Here's the

 <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>

my JS

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.description');

toggleList.addEventListener('click', () => {

                            if (listDiv.style.dispay == 'none') {
                              toggleList.textContent = 'Hide list';
                              listDiv.style.display = 'block';
                            } else {
                              toggleList.textContent = 'Show list';
                              listDiv.style.display = 'none';
                            }
});

Now here's the html on the preview before the button is pressed:

<button id="toggleList" >Hide List</button>
     <div class="list">...</div>

and after

<button id="toggleList" >Show list</button>
     <div class="list" style="display: none;">ā€¦</div>

the ';' just gets added after 'none' and I don't know why, but it's causing the conditional that would should show the text not to be true.

1 Answer

You have a misspelling. dispay should be display here:

if (listDiv.style.dispay == 'none') {

Ah, well there we go. Thanks! Why wouldn't that create an error in the console? I cant't imagine that 'dispay' exists already if it wasn't defined as a CSS style. Unless the the javascript console doesn't care about CSS.