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 trialLukas Korol
2,649 PointsWhat's this function do?
I don't understand this function:
function print(message) {
var div = document.getElementById('output');
div.innerHTML = message;
}
Could someone explain? Why we cannot use simply this:
function print(message) {
document.write(message);
}
1 Answer
miikis
44,957 PointsHey Lucas,
function print(message) {
var div = document.getElementById('output');
div.innerHTML = message;
}
The above function gets the div element with the id of "output" and stores it in the variable called div
. Then it makes the innerHTML
of that div equal to whatever the parameter called message
happens to be. (The value of message
is the argument supplied to the print
function ... once it's called.)
The below function...
function print(message) {
document.write(message);
}
... works too. But it just writes plain text to the screen. That's not exactly practical.
Lukas Korol
2,649 PointsLukas Korol
2,649 PointsThat means: I choose div to put message and by innerHTML I also get all html tags and text inside? What will be when I do without innerHTML element?
miikis
44,957 Pointsmiikis
44,957 Points"What will be when I do without innerHTML element?"
I'm not sure what you mean man... when you do what without the innerHTML method? Why don't you just try it and see what happens?
Lukas Korol
2,649 PointsLukas Korol
2,649 PointsOk sorry for my stupid question. Thank you for your help!