user1854438
user1854438

Reputation: 1842

Array content does not display

I have a div with id wheel, and the content will display going through the loop games[i] where i is 0, but will not shows the content if I append games[0]. Why?

I have included the entire code below. When you click the button it will display 123, but it should display 123 twice, but the games[0] does not append.

  <html>
<head>
<script src="jquery-1.11.2.min.js"></script>
<script>

$(document).ready(function(){

    $("#run").click(function(){

        ReadLine();

    });


});

// ---------------------------------------------------------------
// --------------------- FUNCTIONS -------------------------------
// ---------------------------------------------------------------

function ReadLine()
{

    var games = new Array(new Array());
    var numbers = [1,2,3];
    games.push(numbers);

    for(var i=0; i<games.length; i++)
    {
        $("#wheel").append(games[i]);
    }
        $("#wheel").append(games[0]);
}

</script>


</head>
<body>

<input id="run" type="button" value="Run" onclick="Run();" />
<p>
<div class="container" id="wheel" style="overflow: auto; width:870px;"></div>

</body>
</html>

Upvotes: 1

Views: 33

Answers (1)

lmgonzalves
lmgonzalves

Reputation: 6588

That is because you are creating an Array with an empty Array in first (0) position:

[[]]

Then when you make the push, you get:

[[], [1, 2, 3]]

So, in games[0], there is an empty Array.

See the logs in this DEMO.

Upvotes: 3

Related Questions