Welcome to the Treehouse Community

The Treehouse Community is a meeting place for developers, designers, and programmers of all backgrounds and skill levels to get support. Collaborate here on code errors or bugs that you need feedback on, or asking for an extra set of eyes on your latest project. Join thousands of Treehouse students and alumni in the community today. (Note: Only Treehouse students can comment or ask questions, but non-students are welcome to browse our conversations.)

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and a supportive community. Start your free trial today.

JavaScript JavaScript and the DOM (Retiring) Making Changes to the DOM Styling Elements

Italo Oliveira
Italo Oliveira
4,289 Points

The 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
Steven Parker
218,528 Points

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