BraylanBB121
BraylanBB121

Reputation: 35

Is there a way I can use the .includes() function in JavaScript with an if () statement?

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!');
    }
}

Upvotes: 0

Views: 58

Answers (1)

Barmar
Barmar

Reputation: 781255

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;
}

Upvotes: 1

Related Questions