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 I print the result of the buildList() function directly, without saving it to a variable?

I have the following function, which is used to add content inside a DOM object:

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

I have tried to use it this way:

print( buildList(correct) );

Where 'buildList()' is the function used to create the HTML for the list, and 'correct' is the array with the correct questions. But nothing prints to the page.

If I do as in the video and save everything to the html variable, and then print that variable, it works.

Can someone explain me why?

2 Answers

JavaScript can "display" data in the following ways:

  • Writing into an alert box, using window.alert().
  • Writing into the HTML output using document.write().
  • Writing into an HTML element, using innerHTML.
  • Writing into the browser console, using console.log().

Thanks for your answer, I've updated my question to make clear that the "print" function is actually using "innerHtml" method to write to an html file. This is a question refering to a specific video, I'm not sure if I posted it on the right place...

You can bind your question to special video lesson, if you will create a post on a Question tab below the video ;)

Thanks Sergey, I've tried to do so. Maybe I'd be a good idea to delete this question in order to prevent duplicates, but I can't see a way to do so.

If this question is solved, I guess there are no reason to delete it and create new one. If not, I'm here to help you ;)

Thanks! I don't think it's solved since the innerHTML method is a valid way to write content to a page, still nothing appears when I try to use it the way I explain, that is by passing an array as an argument. Why is it not working?

The reason for deleting it was to post it again under the correct video, so maybe we can delete this one and follow the discussion on the correct one, which I just posted.

Can you provide full code?