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

How does this function work?

Hello everybody,

please can you tell me how the function

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

I mean.. When does the print function is called? At the end of the program a print function is called but does it call this function? How do I pass information to the parameter message? I cannot understand this step! Thanks!

1 Answer

anil rahman
anil rahman
7,786 Points
function print(message) { 
var outputDiv = document.getElementById('output'); 
outputDiv.innerHTML = message; 
}

This is saying that get my div or p tag or w.e with the id of output and store that id reference in a variable callled outputDiv. Then it says on the next line use that reference variable and just a property called innerHTML which means add stuff to my DIV tag so basically write html to the screen. thats where the "= message" bit comes in. It's saying write whatever is stored in this message parameter to the screen in my div tag.

The way message is passed is when you call the function. You can call it wherever you want and more than once.

So in my code lets say i have this:

 var message = "You have now printed to the div tag."; 

This is what we pass to that print function.

print(message);

So now it will take out message from above and say run this print function and will go through the steps for you such as get my output div and write to the screen the message.