skip to Main Content

I am making a game of tic-tac-toe using HTML5, CSS and JavaScript. I have identified the winning patterns and more or less how to make it work. There is no artificial intelligence involved; it is intended to be a player-vs-player-on-the-same-computer game.

enter image description here

All I want to do is:

  • Know when a div is clicked
  • Know which div is clicked
  • Please tell me if that is possible. If yes, how. Steps appreciated, don’t post complete code 🙂

    3

    Answers


    1. It is possible, you should use javascript (JQuery) for this. You can register listeners for mouse click where you can catch the id of div that was clicked

      Login or Signup to reply.
    2. Bind an event listener to the container, then examine the target property of the event object to determine which element was clicked.

      Seriously consider replacing the <div>s with <button>s as they are designed to be interactive (so have benefits such as automatically appearing in the focus order).

      Login or Signup to reply.
    3. this, if you are using jquery, it can be super simple:

      $("div.box").on("click", function() { $(this).addClass("clicked"); });
      

      the above would check for any div with the class “box”, and add a “clicked” class to it

      not sure why this was downvoted.. here is an example showing it working.. even for a sample of what I think was needed.

      http://jsfiddle.net/LCKr6/1/

      Login or Signup to reply.
    Please signup or login to give your own answer.
    Back To Top
    Search