0

how to disable event in jquery ?

ined to disable the cell after click on it i tried with $('.cells').off(); after event.target but it doesn't work
and also i tried with $('.cells').unbind(); after event.target and after click listener but i have the same bugs

    const changeTurn = function Turne() {
        let board, result;
        let player1 = '';
        let player2 = '';
        if (turn === 1) {
            if (player1 !== '' || winningCase(gameBoard()) !== 0 ||player2 !== '') {
               return;
            }
            player1 = 'X'
             event.target.innerHTML = player1
            board = gameBoard();
            result = winningCase(board);
            if (result !== 0) {
                gameOver(result);
                return;
            }
            turn++
            moveCuont++
} else {

            if (player1 !== '' || winningCase(gameBoard()) !== 0 ||
                player2 !== '') {
                return;
            }
            player2 = 'O'
            event.target.innerHTML = player2
            board = gameBoard();
            result = winningCase(board);
            if (result !== 0) {
                gameOver(result);
                return;
            }
           turn--
           moveCuont++
            eve.off('click')
        }
        result = checkWinner(board);
        if (moveCuont === 9) {
            gameOver(result);
            return;
        }
    }
    $(".cells").click(changeTurn)

Omar M H
  • 3
  • 2
  • 1
    You can use [off](https://api.jquery.com/off/) to turn the event listener off. Syntax would look like `$(".cells").off('click', changeTurn);` – David784 Feb 02 '20 at 17:59
  • Welcome to Stack Overflow. As was mentioned, you have to define the event name that you want to turn off with you use `.off()`. In your script, you do not define `event` nor `eve`, so the `event.target` and `eve.off()` would both fail. – Twisty Feb 02 '20 at 18:07

1 Answers1

0

I would advise the following code.

function changeTurn(event) {
  var board, result;
  var player1 = '';
  var player2 = '';
  var self = $(event.target);
  if (turn === 1) {
    if (player1 !== '' || winningCase(gameBoard()) !== 0 ||player2 !== '') {
      return false;
    }
    player1 = 'X'
    self.html(player1);
    board = gameBoard();
    result = winningCase(board);
    if (result !== 0) {
      gameOver(result);
      return false;
    }
    turn++;
    moveCuont++;
  } else {
    if (player1 !== '' || winningCase(gameBoard()) !== 0 ||
      player2 !== '') {
      return false;
    }
    player2 = 'O'
    self.html(player2);
    board = gameBoard();
    result = winningCase(board);
    if (result !== 0) {
      gameOver(result);
      return false;
    }
    turn--;
    moveCuont++;
    self.off('click');
  }
  result = checkWinner(board);
  if (moveCuont === 9) {
    gameOver(result);
    return false;
  }
}
$(".cells").click(changeTurn);

You had a number of syntax issues, missing ;. Also since your function did not define an event parameter, it would not be passed into the function on callback, so you would not have an event variable to reference, it would be null or undefined.

In regards to return false, I would review: Is it better to return `undefined` or `null` from a javascript function?

Twisty
  • 30,304
  • 2
  • 26
  • 45