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

Can anyone explain all the pieces of the print function in this video? Particularly getElementById and innerHTML.

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

This seems like a fundamental part of javascript, but I am not yet understanding all of the pieces.

1 Answer

Hello Zach,

The function works as follows

//the function is declared with the name of print
function print(message) {
   // a variable is created and labeled 'outputDiv'
   // the variable is then pointing to an html element with the ID of output
   var outputDiv = document.getElementById("output");
   //the html element is then targeted and given text, using the given argument when the print function is called
   outputDiv.innerHTML = message;
}

//calling the print message and passing an argument
print("Hello, World!"); // "Hello, World" = message

I hope this helps.

Thanks this will a great explanation!