document.getElementById(...) is null or not an object....but it is?

cassidymack picture cassidymack · Jan 13, 2013 · Viewed 8.1k times · Source

Hey so I'm a bit confused as to why I'm getting the error, 'document.getElementById(...)' is null or not an object. I'm new to javascript and practicing writing arrays. This script worked fine before when I simply had document.write(r_card[i]); and no function. The page would load and randomly pick a card. But then I wanted to practice with functions and calling them so I added the function bar() { part and changed document.write(r_card[i]); to document.getElementById(foo).innerHTML = (r_card[i]); but now it's not working. Usually I get syntax errors when I run into problems like this because I'm new to writing javascript on my own. Did some searching but couldn't find anyone with the same error.

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">

<head>
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
    <meta name="description" content="" />
    <meta name="keywords" content="" />
    <title>Pick a Card!</title>
</head>

<body>
    <script type="text/javascript">
        function bar() {
            var r_card = new Array();
            r_card[0] = "Ace of Diamonds";
            r_card[1] = "Two of Diamonds";
            r_card[2] = "Three of Diamonds";
            r_card[3] = "Four of Diamonds";
            r_card[4] = "Five of Diamonds";
            r_card[5] = "Six of Diamonds";
            r_card[6] = "Seven of Diamonds";
            r_card[7] = "Eight of Diamonds";
            r_card[8] = "Nine of Diamonds";
            r_card[9] = "Ten of Diamonds";
            r_card[10] = "Jack of Diamonds";
            r_card[11] = "Queen of Diamonds";
            r_card[12] = "King of Diamonds";
            r_card[13] = "Ace of Clubs";
            r_card[14] = "Two of Clubs";
            r_card[15] = "Three of Clubs";
            r_card[16] = "Four of Clubs";
            r_card[17] = "Five of Clubs";
            r_card[18] = "Six of Clubs";
            r_card[19] = "Seven of Clubs";
            r_card[20] = "Eight of Clubs";
            r_card[21] = "Nine of Clubs";
            r_card[22] = "Ten of Clubs";
            r_card[23] = "Jack of Clubs";
            r_card[24] = "Queen of Clubs";
            r_card[25] = "King of Clubs";
            r_card[26] = "Ace of Hearts";
            r_card[27] = "Two of Hearts";
            r_card[28] = "Three of Hearts";
            r_card[29] = "Four of Hearts";
            r_card[30] = "Five of Hearts";
            r_card[31] = "Six of Hearts";
            r_card[32] = "Seven of Hearts";
            r_card[33] = "Eight of Hearts";
            r_card[34] = "Nine of Hearts";
            r_card[35] = "Ten of Hearts";
            r_card[36] = "Jack of Hearts";
            r_card[37] = "Queen of Hearts";
            r_card[38] = "King of Hearts";
            r_card[39] = "Ace of Spades";
            r_card[40] = "Two of Spades";
            r_card[41] = "Three of Spades";
            r_card[42] = "Four of Spades";
            r_card[43] = "Five of Spades";
            r_card[44] = "Six of Spades";
            r_card[45] = "Seven of Spades";
            r_card[46] = "Eight of Spades";
            r_card[47] = "Nine of Spades";
            r_card[48] = "Ten of Spades";
            r_card[49] = "Jack of Spades";
            r_card[50] = "Queen of Spades";
            r_card[51] = "King of Spades"
            var i = Math.floor(52 * Math.random())
            document.getElementById(foo).innerHTML = (r_card[i]);
        }
    </script>
    <input type="button" value="Pick a Card!" onclick="bar()" />
    <br />
    <div id="foo">Test</div>
</body>

</html>

Any help you can offer would be greatly appreciated!

Answer

Denys S&#233;guret picture Denys Séguret · Jan 13, 2013

Replace

document.getElementById(foo).innerHTML = (r_card[i]);

with

document.getElementById('foo').innerHTML = (r_card[i]);

You have no variable named foo.

As a side note, you could make the array declaration cleaner with

var r_card = [
     "Ace of Diamonds",
     "Two of Diamonds"
     ...
];