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

how can I access .style in a variable

I have this code:

            const pWarning = picBox.querySelector('p').style.display = 'block';
            setTimeout(() => pWarning.style.display = 'none', 3000)

how can I access the pWarning.style.display and change it to none, because this is wrong and its not working ^, do i really need to right 3 lines of code? like that

            const pWarning = picBox.querySelector('p')
            pWarning.style.display = 'block';
            setTimeout(() => pWarning.style.display = 'none', 3000)

I'm sure there is a better way, how can i access the variable..? thanks a lot ^-^ have a good day.

1 Answer

I don't know if its allowed to chain "=" like that and what would be the result.

const pWarning = picBox.querySelector('p').style.display = 'block';

Your second version looks good to me. It is as simple as it gets. Just don't forget the semicolons on the first and last row.