Welcome to the Treehouse Community
The Treehouse Community is a meeting place for developers, designers, and programmers of all backgrounds and skill levels to get support. Collaborate here on code errors or bugs that you need feedback on, or asking for an extra set of eyes on your latest project. Join thousands of Treehouse students and alumni in the community today. (Note: Only Treehouse students can comment or ask questions, but non-students are welcome to browse our conversations.)
Looking to learn something new?
Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and a supportive community. Start your free trial today.

Yannis Bouacida
9,169 PointsWill this code work ?
drawHTMLToken(){
var token = document.createElement('div');
var gameBoard = document.getElementById('game-board-underlay');
gameBoard.appendChild(token);
div.setAttribute('id',this.id);
div.setAttribute('class', 'token');
div.style.backgroundColor = this.owner.color;
}
4 Answers

Michael Cook
Full Stack JavaScript Techdegree Graduate 28,962 PointsHey Yannis, your method is definitely syntactically correct, but I do think there are some issues.
You have defined a variable called
token
and set it equal to the creation of a<div>
element. So far so good.You append
token
to the game board. Still good.Now here is the problem, I think. You try to set attributes and CSS on the token, but you're calling the
setAttribute
method on<div>
rather than calling it on the variable pointing to the<div>
, which would betoken
. You also set the attributes after you have appended the token, which isn't really a problem but I think if it were me I would append the token after setting all of its attributes, just for the sake of having a logical order.
Try running your function with
div.setAttribute('id',this.id);
div.setAttribute('class', 'token');
div.style.backgroundColor = this.owner.color;
replaced as
token.setAttribute('id',this.id);
token.setAttribute('class', 'token');
token.style.backgroundColor = this.owner.color;
If this doesn't work for you, I apologize. I think it will but its been a bit since I touched any DOM scripting. Happy coding!

Stephen Hanlon
11,845 PointsIn the spirit of ES6 syntax and the template literals discussed earlier, i found this to be a little easier to understand creating the Tokens in place of createElement
drawHTMLToken() {
const gameBoard = document.getElementById('game-board-underlay');
const token = `<div id="${this.id}" class="token" style="background-color:${this.owner.color}"></div>`;
gameBoard.innerHTML += token;
}

Yannis Bouacida
9,169 PointsThank you very much for the answer!

Yannis Bouacida
9,169 PointsI will, I’m actually working on it! Thank you very much for the feedback
Michael Cook
Full Stack JavaScript Techdegree Graduate 28,962 PointsMichael Cook
Full Stack JavaScript Techdegree Graduate 28,962 PointsAlso, I would try to get into the habit of using the new ES syntax. Change your
var
keywords toconst
.