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 Object-Oriented JavaScript: Challenge Making the Game Interactive Build the moveLeft() and moveRight() Methods

Inside moveLeft() and moveRight(), instead of updating htmlToken.style.left, why don't we update htmlToken.offsetLeft?

Before seeing the solution, I thought these methods would be done using the Node Element property 'offsetLeft' instead of using the CSS property 'left'.

Would it be correct to use this other way? If so, there's a reason to why she didn't use it?

Thank you!

1 Answer

Steven Parker
Steven Parker
231,275 Points

The instruction does use the "offsetLeft" property to get the current position. But to change the position, the "left" CSS style property is used.

This is because "offsetLeft" is a read-only property and cannot be changed directly. For more details, see the MDN page on HTMLElement.offsetLeft .

Thanks, Steven! I did read that offsetLeft is a read-only property, but I didn't connect the dots :/ Thank you very much for taking the time and clearing that up!