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
Liam Hayes
Full Stack JavaScript Techdegree Student 13,116 PointsI don't know how to get Professor McFarland's print function to work
How's it going guys? =)
This is a function that Professor McFarland talks about in one of the videos:
function print(message)
{
var div = document.getElementById('output');
div.innerHTML = message;
}
He says that it is superior to writing document.write("hello world");. I want to use it but I can't get it to work. For example, if I call it like this print("hello world");. the following error appears:
- Error in this line: div.innerHTML = message;
- Uncaught TypeError: Cannot set property 'innerHTML' of null
Does anybody know what I can do to make this function work so I can use it instead of using document.write()?
Thank you!!
1 Answer
Steven Parker
243,228 PointsCheck your HTML code. That's the error I would expect to see if there is not any element which has the "id" attribute set to "output".
Your HTML should include something like this:
<div id="output"></div>
Liam Hayes
Full Stack JavaScript Techdegree Student 13,116 PointsLiam Hayes
Full Stack JavaScript Techdegree Student 13,116 PointsI will try that. Thank you Steven :)