1

I'm new to web programming. I have tried to draw a table row with JavaScript but it's not working and I don't' know why.

here's the code

<div id="gameDiv"> </div>
<script type="text/javascript">
    public function drawGame(){ 
        var table = document.createElement('table');
        table.setAttribute('style','float:left');
        var startRow = table.insertRow(0);
        var c = 'A'
        for (j=0; j<8; j++) {
            var text = document.createTextNode(c++);
            var cell = startRow.insertCell(j);
            cell.appendChild(text);
        }
        document.getElementById("gameDiv").appendChild(table);
    }
    $(document).ready(function() {
        drawGame();
    };
</script>
Joeytje50
  • 18,636
  • 15
  • 63
  • 95
  • 3
    JavaScript doesn't currently use access modifiers, like `public`, as you might find in other languages; there's a closing `)` missing for `.ready()` between the `};`; and string values like `'A'` don't support incrementing, as `c++` is attempting. – Jonathan Lonowski Jun 03 '14 at 22:11
  • 2
    Always open your browser console while working with JS. – Rafael Adel Jun 03 '14 at 22:14
  • 2
    The variable c is also a string, and you cannot increment it with ++. – Andy G Jun 03 '14 at 22:15
  • thank you guys, now I have a new problem. how can I show row with alphabets in an incremented way. something like A B C D F... and do it dynamically with a loop – user3503716 Jun 03 '14 at 22:27
  • @user3503716 http://stackoverflow.com/questions/12504042 (see also my answer ;) ) – Dog Ears Jun 03 '14 at 22:31

4 Answers4

5

The problem is the fact you use public. Javascript doesn't have public or private, so putting that in front of your function declaration will cause errors. If you open up your console, you'll see an error like:

SyntaxError: Unexpected token function

So to fix this, simply remove the private modifier from your code.

Also, you seem to be missing a closing parenthesis at the end of your code. Instead, you should use this:

$(document).ready(function() {
    drawGame();
});

but that code could also be written much shorter:

$(drawGame);
Joeytje50
  • 18,636
  • 15
  • 63
  • 95
0

Well, you're getting an error because I think you might be getting your languages mixed up, there's no need to declare a function as public in javascript, it will give you an error.

The drawGame function can just be:

    function drawGame(){ 
        var table = document.createElement('table');
        table.setAttribute('style','float:left');
        var startRow = table.insertRow(0);
        var c = 'A'
        for(j=0; j<8; j++){
            var text = document.createTextNode(c++);
            var cell = startRow.insertCell(j);

            cell.appendChild(text);

        }
}
edwh
  • 58
  • 1
  • 5
0

Here is kind of working variant http://jsbin.com/hisewamu/4/edit, "$" notion is part from jquery that you should include

Yanaki
  • 254
  • 2
  • 4
0

This should get rid of that NaN (Not a Number) nonsense.

<script>
        function drawGame(){ 
        var table = document.createElement('table');
        table.setAttribute('style','float:left');
        var startRow = table.insertRow(0);
        var c = 'A'.charCodeAt()
        for(j=0; j<8; j++){
            var text = document.createTextNode(String.fromCharCode(c++));
            var cell = startRow.insertCell(j);

            cell.appendChild(text);

        }
    document.getElementById("gameDiv").appendChild(table);

    }

    $(document).ready(function() {
    drawGame();

    });
</script>
Dog Ears
  • 9,637
  • 5
  • 37
  • 54