Working on a simple jquery tic-tac-toe project. I'm trying to iterate through the board and save the values of each cell in an array, but can't figure out how to access the text inside each td. I've tried .text() and .value() but both return undefined
index.html:
<html>
<head>
<title>jQuery Tic Tac Toe</title>
</head>
<body>
<table border="1" cellpadding="40">
<tr>
<td data-x="0" data-y="0">Test</td>
<td data-x="1" data-y="0"></td>
<td data-x="2" data-y="0"></td>
</tr>
<tr>
<td data-x="0" data-y="1"></td>
<td data-x="1" data-y="1"></td>
<td data-x="2" data-y="1"></td>
</tr>
<tr>
<td data-x="0" data-y="2"></td>
<td data-x="1" data-y="2"></td>
<td data-x="2" data-y="2"></td>
</tr>
</table>
<div id="games"></div>
<div id="message"></div>
<button id="save">Save Game</button>
<button id="previous">Show Previous Games</button>
</body>
</html>
tic-tac-toe.js:
function getBoard(){
board = [];
$.each($("td"), function(index, cell){
board.push(cell); //Need to return contents of cell
});
alert(board);
}
Try this:-
Use this cell.innerHTML
instead of cell
function getBoard() {
board = [];
$.each($("td"), function (index, cell) {
board.push(cell.innerHTML); //Need to return contents of cell
});
alert(board);
}
Hope this will help you.
You need to use .text()
$.each("td", function(index, cell) {
board.push($(cell).text()); //Need to return contents of cell
});
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With