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 Loops, Arrays and Objects Tracking Data Using Objects Accessing Object Properties

function print (message) { }

I was wondering if anyone remembers where this is discussed?

function print(message) { var div = document.getElementById('output'); div.innerHTML = message; }

I've been searching for awhile and have come up short. Or if anyone has some incite on their understanding of it that would be even better.

Thanks in advance, Matt

Yes, much clearer now thank you. Just have to use it in some instances to fully understand it.

2 Answers

Felix Sonnenholzer
Felix Sonnenholzer
14,654 Points

Hi there,

the video where it is introduced is https://teamtreehouse.com/library/build-a-quiz-challenge-part-2

But I try to explain it as well:

var div = document.getElementById('output');

document refers to your html and you are looking for a element with an id='output' and store that in the variable. Now we set the 'inner' html of said element to the content of message

<div id="outer-element">
  <div id="here-starts-the-inner-html-from-the-outer-element">
    <p>And some more innerHtml from outer element</p>
  </div>
</div>

So innerHtml references any html content inside your chosen element. In this case it would be the inner div and the p.

You could say innerHtml refers to all the children of the outer-element.

e.g. If I do this:

var div = document.getElementById('outer-element');
div.innerHtml = '<div id="another-element-id"><p>And a changed p tag</p></div>'

Then the above html looks like this:

<div id="outer-element">
  <div id="another-element-id">
    <p>And a changed p tag</p>
  </div>
</div>

Is it clear now?

Mwalima Peltenburg
Mwalima Peltenburg
2,693 Points

the element in the video in the index.html is not id='output' but id = 'listDiv' if jou call function print(message){ var div = document.getElementById('listDiv'); div.innerHTML = message; }; it will work

Muhammad Umar
Muhammad Umar
7,817 Points

Thank you for pointing that out. I noticed it and was wandering if someone else also caught it :)