0

I am trying to create divs when another div is clicked, but nothing is happening, and the console says nothing. I have done research and haven't been able to find anything on this. Any help would be greatly appreciated.

HTML

<!DOCTYPE html>
<html>
    <head>
        <title>
        Space Invaders
        </title>
        <script type = 'text/javascript' src = 'http://ajax.googleapis.com/ajax/libs/jquery/1.9.1/jquery.min.js'></script>
        <link href="style.css" rel="stylesheet" type="text/css" />
        <script src="script.js" type="text/javascript">               </script>
    </head>
    <body>
    <div>
        <div id="startbtn"><br />Start Game
        </div>
        <br />
        <center>
        <div id="game" onClick = 'buttonClick'>
        </div>
        <div id="game2">
        </div>
        </center>
        <div id="titlepage">
        </div>
    </div>
    </body>
</html>

CSS

#startbtn
{
    height: 60px;
    width: 100px;
    border-radius: 5px;
    background-color: #69D2E7;
    text-align: center;
    color: #000000;
    font-family: Arial;
    opacity: 0.5;
    margin: auto;
}
#game { 
    display: none;
    margin: auto;
}
#game2
{ 
    display: none;
    margin: auto;
}
#grid {
    display: inline-block;
    background-color: #000000;
    width: 20px;
    height: 20px;
    border-radius: 2px;
}

JavaScript

var row1 = [];
$(document).ready(function() 
{
    $('#startbtn').click(function(){
        for (var i = 0; i<9; i++)
    {
        row1[i] = document.createElement('div');
        row1[i].id = 'grid';
        document.getElementById('game').appendChild(row1[i]);
        }
    }   
    });
});
2
  • 3
    Id's must be unique.... Commented May 30, 2014 at 19:17
  • 1
    You have posted the Javascript in the HTML's place Commented May 30, 2014 at 19:19

2 Answers 2

2
for (var i = 0; i<9; i++)
{
    row1[i] = document.createElement('div');
    row1[i].id = 'grid';

The id attribute needs to be unique within the document. You can't create 9 divs with the same id of grid.

Sign up to request clarification or add additional context in comments.

Comments

0

You have to set unique id's. An easy way to do so, is to add the count variable on the id:

    for (var i = 0; i<9; i++)
    {
        row1[i] = document.createElement('div');
        row1[i].id = 'grid' + i;
        document.getElementById('game').appendChild(row1[i]);
    }

EDIT

I've noticed what might have confused you. You wanted the div's ids to be grid, so you would style them all the same. I took that from your css #grid.

You should do that by css classes. Change the css style from #grid to .grid, then add this class to the created divs (Remember to make the ids unique):

    for (var i = 0; i<9; i++)
    {
        row1[i] = document.createElement('div');
        row1[i].id = 'grid' + i;
        row1[i].className = 'grid';
        document.getElementById('game').appendChild(row1[i]);
    }

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.