Thank you in advance for your help!
I'm creating a simple tic-tac-toe game to help me learn native JavaScript, so my goal is to do as much as I can without jQuery or HTML/CSS for the game.
I am using a for-loop
to create multiple div
elements, adhering to JSLint. I am attaching an .addEventListener()
as part of the process of creating my div
elements to change the background color of that specific div
when clicked.
I've been searching StackOverflow trying to use this
to reference the specific div
clicked. The only way I've been successful so far is by using an anonymous function within my for-loop
. JSLint isn't impressed, and I get:
Don't make functions within a loop.
When I try to call an external function and pass this
in, the entire div
creation process just stops working and I'm not sure why.
What I have (that "works"): https://jsfiddle.net/typj2LLb/4/
// create game
var gameContainer = document.getElementById('board');
var createBoard = function() {
'use strict';
var index, square;
for (index = 0; index < 9; index += 1) {
square = document.createElement('div');
square.className = 'tile';
// tile event
square.addEventListener('click', function() {
this.style.backgroundColor = 'yellow';
});
gameContainer.appendChild(square);
}
};
createBoard();
.tile {
display: inline-block;
height: 25vh;
width: 30%;
margin: 0 3px;
border: 1px solid black;
}
<body>
<div id="board"></div>
</body>
What I think I'm supposed to be doing (that doesn't work): https://jsfiddle.net/e4mstyy9/1/
// click-event
function changeColor(specificElement) {
'use strict';
specificElement.style.backgroundColor = 'yellow';
}
// create game
var gameContainer = document.getElementById('board');
var createBoard = function() {
'use strict';
var index, square;
for (index = 0; index < 9; index += 1) {
square = document.createElement('div');
square.className = 'tile';
// tile event
square.addEventListener('click', changeColor(this));
gameContainer.appendChild(square);
}
};
createBoard();
.tile {
display: inline-block;
height: 25vh;
width: 30%;
margin: 0 3px;
border: 1px solid black;
}
<body>
<div id="board"></div>
</body>