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 Multiple Items with Arrays Build a Quiz Challenge, Part 2 Solution

What's this function do?

I don't understand this function:

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

Could someone explain? Why we cannot use simply this:

function print(message) {
  document.write(message);
}

1 Answer

Hey Lucas,

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

The above function gets the div element with the id of "output" and stores it in the variable called div. Then it makes the innerHTML of that div equal to whatever the parameter called message happens to be. (The value of message is the argument supplied to the print function ... once it's called.)

The below function...

function print(message) {
  document.write(message);
}

... works too. But it just writes plain text to the screen. That's not exactly practical.

That means: I choose div to put message and by innerHTML I also get all html tags and text inside? What will be when I do without innerHTML element?

"What will be when I do without innerHTML element?"

I'm not sure what you mean man... when you do what without the innerHTML method? Why don't you just try it and see what happens?

Ok sorry for my stupid question. Thank you for your help!