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

Alex Flores
Alex Flores
7,864 Points

Can someone explain to me getElementByID and the function(message)?

I don't really understand the purpose of var function (message); I tried deleting it and seeing what happened in the browser and after I finished the quiz it actually prompted me to print the page. So my guess is that the var function (message) makes it so that it is printed to the page. Right?

So, why is this the case? Why can't we add document.write(variable) inside the code? Is it just because we can create a node, which would make it easier to change the code later?

Also, where does this "message" from var function(message) come from? Is this a javascript term or could I use anything in here (var function(bananas ))?

Thanks in advance!

2 Answers

Mike Mitchell
PLUS
Mike Mitchell
Courses Plus Student 27,026 Points

Hey Alex, You can add document.write(variable); but one benefit of having a function that manipulates the DOM is it gives you the ability to style the output a certain way without having to add that style to the JavaScript. You can leave the style in the CSS.

The message from function(message) is an argument holder so that you can later call the function with a specific argument.

For example:

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

bananas = 'This ish is bananas, BANANAS!'
print(bananas);

In order to fire off that print function, I need to supply some kind of message. I create a message, in this case a message called bananas, and drop it into the function.

I can later create a different message called something else and drop it into the print function for it to do the same thing.

Does that make sense? I'm still learning as well, but this is how I understand it.

Best, Mike

Mike Mitchell
Mike Mitchell
Courses Plus Student 27,026 Points

Was really hoping the markdown would work...Let me see if I can edit my response.

William Ruiz
William Ruiz
10,027 Points

I'd like to add a reference to MDN on the DOM's .getElementbyId(). I've found their site invaluable to cementing my use of stuff. Manipulating the DOM is also new to me but I guess this is Dave's ways of wetting our feet.

Alex Flores
Alex Flores
7,864 Points

Mike, thanks for taking the time to explain that. It was a great explanation!