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 trialItalo Oliveira
4,289 PointsThe code doesn't work when I try to make simplify my code.
I can't understand why my code (the button) doesn't work when I write it in another way trying to simplify it:
toggleList.addEventListener('click', () => { let listDisplay = listDiv.style.display; let buttonName = toggleList.innerHTML; if (listDisplay == 'none') { buttonName = 'Hide list'; listDisplay = 'block'; } else { buttonName = 'Show list'; listDisplay = 'none'; }});
1 Answer
Steven Parker
231,261 PointsWhen you assign a variable with an element property, the variable stores the value of the property, not a reference to it. So later, if you change what is in the variable, it will not affect the property of the element or what is shown on the page.
You didn't show the "before" code, but I'd bet originally it was making changes directly to the element properties.