Q:

Declaring a winner in tic tac toe

I'm writing a tic-tac-toe program.  So far I have the 3x3 grid, and I'm able to drag and drop X and O images into the grid.
To declare a winner, would I be using an if else statement?
I'm just wondering how I can declare a winner.  I'm guessing this requires javascript.
Here's my code in jsfiddle, as well as what I have compiled below:
http://jsfiddle.net/VXQ7P/

<!DOCTYPE HTML>
<html>
<head>

<style type="text/css">

  #div1, #div2, #div3, #div4, #div5, #div6, #div7, #div8, #div9 {
      width: 80px;
      height: 80px;
      padding: 10px;
      border: 1px solid #aaaaaa;
      float: left;
  }
  
</style>

 <script type="text/javascript">

    function allowDrop(ev) {
        ev.preventDefault();
    }

    function drag(ev) {
        ev.dataTransfer.setData("Text",ev.target.id);
        helper:clone;
    }

    function drop(ev) {
        var data=ev.dataTransfer.getData("Text");
        ev.target.appendChild(document.getElementById(data).cloneNode(true));
        ev.preventDefault();
    }
</script>

<title>JavaScript  Drag &amp; Drop Tic-Tac-Toe</title>
</head>

<body>
    <p>Drag the X and O images into the tic-tac-toe board:</p>
    
    <table>
        <tr>
            <td><div id="div1" ondrop="drop(event)" ondragover="allowDrop(event)"></div></td>
            <td><div id="div2" ondrop="drop(event)" ondragover="allowDrop(event)"></div></td>
            <td><div id="div3" ondrop="drop(event)" ondragover="allowDrop(event)"></div></td>
        </tr>
        <tr>
            <td><div id="div4" ondrop="drop(event)" ondragover="allowDrop(event)"></div></td>
            <td><div id="div5" ondrop="drop(event)" ondragover="allowDrop(event)"></div></td>
            <td><div id="div6" ondrop="drop(event)" ondragover="allowDrop(event)"></div></td>
        </tr>
        <tr>
            <td><div id="div7" ondrop="drop(event)" ondragover="allowDrop(event)"></div></td>
            <td><div id="div8" ondrop="drop(event)" ondragover="allowDrop(event)"></div></td>
            <td><div id="div9" ondrop="drop(event)" ondragover="allowDrop(event)"></div></td>
        </tr>
    </table>

    <img id="drag1" src="X.png" ondragstart="drag(event)" width="75" height="75"/>
    <img id="drag2" src="O.png" draggable="true" ondragstart="drag(event)" width="75" height="75"/> 
</body>
</html>

A:

In my opionion you are going a wron way there. You should use "onclick()" for every field and then change the innerhtml of the div to X or O. You might also want to google for "tic tac toe javascript". A lot of people did this before. Check out this one for example:  http://jsfiddle.net/rtoal/ThPEH/
The way the winner is defined is very easy to understand and well explained there. ;)
Grabed from the site:
/*
 * To determine a win condition, each square is "tagged" from left
 * to right, top to bottom, with successive powers of 2.  Each cell
 * thus represents an individual bit in a 9-bit string, and a
 * player's squares at any given time can be represented as a
 * unique 9-bit value. A winner can thus be easily determined by
 * checking whether the player's current 9 bits have covered any
 * of the eight "three-in-a-row" combinations.
 *
 *     273                 84
 *        \               /
 *          1 |   2 |   4  = 7
 *       -----+-----+-----
 *          8 |  16 |  32  = 56
 *       -----+-----+-----
 *         64 | 128 | 256  = 448
 *       =================
 *         73   146   292
 *
 */
wins = [7, 56, 448, 73, 146, 292, 273, 84],

edit:
You could store the state and the value of each TicTacToe-cell in an array. This way you can easily access them to determine if there is a winner.
http://pastebin.com/xDhBUfeS

