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

Semi Lee
Semi Lee
5,284 Points

I added class (.description) to const input and const button because two addEventListener is fire. after, not working

Everything was okay but I added class (.description) to const input and const button, and there is something wrong. Please check the codes.

<!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> <p class="description">Things that are purple:</p> <input style="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>

const toggleList = document.querySelector('#toggleList'); const div = document.querySelector('div'); const input = document.querySelector('input.description'); const p = document.querySelector('p.description'); const button = document.querySelector('button.description');

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

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

Same exact thing happened to me. I checked every line of code against Guil's and mine was the same but I have the same issue as you, the click/toggle stopped working. Did you ever figure out why? I'd love to know!

I figured it out in the next video. I don't know if he changed it and the video did not show us between this one and the next, but where you and I both had:

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

It should be:

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

toggleList instead of button at the beginning is what the issue was.

1 Answer

Steven Parker
Steven Parker
231,271 Points

Both handlers are being added to "button", but the button for hiding the list is "toggleList".