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

Why the text does not show up in the browser

Hello, I have the below code:

code

<!DOCTYPE html>
<html>
<head>

</head>
<body>

    <h1 id="hello">Hommmmmmmmmmmmmmmmmmmmmmmmmmmmmmmme</h1>
    <p>This is the home page.</p>

    <script>
        document.getElementById('hello').innerHTML = 'Hello';
    </script>

</body>
</html>

I was expecting that when the page loads to see the text 'Hommmmmmmmmmmmmmmmmmmmmmmmmmmmmmmme' first, than seeing the text 'Hello' being replaced by it. Why is it that when I load the page, I see the text 'Hello', but I don't see the text 'Hommmmmmmmmmmmmmmmmmmmmmmmmmmmmmmme' prior to it which gets replaced by the text 'Hello'? Doesn't html elements get loaded first and get displayed first, and then Javascript runs and manipulates the elements that already are being displayed by the html code?

1 Answer

Hi Reza,

You're correct, the html loads first and then when it reaches the script tag your JavaScript runs. The thing is that this happens really fast, which is why you can't see your first text.

I hope this helps :)

Jonas