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 The Build an Object Challenge, Part 2 Solution

Justin Houghton
Justin Houghton
4,560 Points

Print Function + document.getElementById + innerHTML clarity

This might seem redundant but I just want to talk my way through this function so that I make sure I understand each piece. Please let me know if I am misunderstanding anything!

function print(message) {
  var div = document.getElementById("output");
  div.innerHTML = message;
}
  1. We are calling on a function named print and passing whatever is in between the { } into the variable "message"

  2. We declare that the element with the id of "output" is now equal to the variable we are calling div.

  3. We are saying that the inner html of the variable "div" (which is equal to the contents of the div in the document with an id of "output") is equal to the variable "message".

So when we call the function....

print(message);

We are saying to print out the contents of the variable "message" into the inner html of the div with an id of "output"?

1 Answer

Bruno Navarrete
seal-mask
.a{fill-rule:evenodd;}techdegree seal-36
Bruno Navarrete
Full Stack JavaScript Techdegree Graduate 22,246 Points

Your thinking is absolutely right, the only thing wrong with step 1 is that you used {} instead of parenthesis when asking the question but that's actually irrelevant ;P

// this will print the given string into #output
    print('irrelevant advice');

// this will print the given number into #output
    print(6);

// this will print the _message_ variable value
   print(message);

and so on...

Justin Houghton
Justin Houghton
4,560 Points

Thank you! I have a long way to go, but it's really awesome to have this treehouse community to bounce things back for clarity.