0

I am trying to code a tic tac toe game on repl.it. I am trying to code the if (space owned) then part, but I need to find out if a p element includes certain characters to determine if the space is owned. But when I do that, it says in the console, TypeError: document.getElementById(...).includes is not a function at tac (/code.js:4:35) at HTMLButtonElement.onclick (/:27:27) What code should I use to fix that? The code I am using is:

function tac() {
    var tac = prompt('Type the first letter of each word (e.g. If you want the top left, type tl)');
    var playerName = prompt('Are you player 1 or 2?');
    if (document.getElementById(tac).includes(' is availible')) {
        document.getElementById(tac).innerHTML = 'This space is owned by ' + playerName;
    } else {
        alert('That space is already taken!');
    }
}
3
  • You get that error because whatever document.getElementById(...) returns does not implement an includes function. In other word - it's not a string. Commented Apr 13, 2020 at 21:44
  • You probably meant to call .includes() on the .textContent of the element (a string), not on the element itself. Commented Apr 13, 2020 at 21:45
  • 1
    So you mean I should use document.getElementById(...).textContent.includes(); And does the .includes() function return a boolean? Like true or false? Commented Apr 13, 2020 at 21:48

1 Answer 1

1

You need to check the text of the element, not the element itself.

if (document.getElementById(tac).innerText.includes(' is availible')) {
    document.getElementById(tac).innerText = 'This space is owned by ' + playerName;
}
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.