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

What's the difference while using innerHTML() method?

Hi! I can't quite understand why this piece of code does work:

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

but the code below, which is for me is looking pretty the same, doesn't:

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

Pls, help! =)

1 Answer

Umesh Ravji
Umesh Ravji
42,386 Points
var outputPlace = document.getElementById( 'output' );

This line assigns the element to the variable.

 outputPlace.innerHTML = message;

This line sets the elements innerHTML property to the contents of the message variable.

Compared too..

var outputPlace = document.getElementById( 'output' ).innerHTML;

This line places the value of the innerHTML property into the variable.

outputPlace = message;

This line just replaces the variable with the contents of the message variable.

I hope that is enough :)

Great answer! Thanks! =)