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

deepak sharma
deepak sharma
3,844 Points

In this styling elements section why use ( listDiv.style.display = 'block'; ). why use it display to block.why?

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'; }); question: what is the reason behind using display block?

2 Answers

Bogdan Cabaj
Bogdan Cabaj
16,348 Points

DIV by default has a display=block. Since you are setting it to none (hiding the div) you have to set it back to block if you want to show it.

ywang04
ywang04
6,762 Points

I see. Thanks a lot.

Actually, I'm not sure whether you are asking "Why use 'block' as opposed to (maybe) 'inline' or 'block' as opposed to 'none'. My guess is from the limited context that it is, as opposed to 'none', in which case, I agree with Bogdan. The 'none' hides the object and you need a way to 'unhide' the object. That would be 'block' or another of the visible display properties like 'inline' or 'inline-block'. But probably 'block'. Sorry, if I seem to be equivocating. Its just such limited context.