1

Ok, I read this thread to figure out how to generate HTML with JavaScript. I attempted doing it with the following script:

<script type='text/javascript'>
function generate_page() {
    var x = 0;
    var y = 0;
    var lines = 20;
    var output;
    while (x < lines) {
        while( y < lines*2){
            output = ("<div id='x" + x + "_" + y + "'>x</span>");
            $('board').prepend(output);
            y++;
        }
        y = 0;
        x++;
        $('board').append('<br />');
    }
}
</script>

</head>
<input type='button' value='test' onClick='generate_page()'>
<body>
<div id='board'>

</div>

</body>
</html>

It doesn't return any errors but just simply doesn't do anything. What am I doing wrong?

5
  • a) you're using jQuery syntax so are you including jQuery and b) why is your input outside the body tag? Commented Jun 14, 2012 at 2:43
  • $('board') ==> $('#board') Commented Jun 14, 2012 at 2:46
  • The title of this post should be "Writing HTML with JavaScript"... Commented Jun 14, 2012 at 2:47
  • 1
    @ikaradashkov Why should it be called that? Commented Jun 14, 2012 at 2:49
  • @GerardSexton Because the title used to be "Inserting JavaScript in HTML". Now, I see that it has been modified to "Inserting HTML using JavaScript" which makes total sense (and is along the lines of what I had proposed). Commented Jun 14, 2012 at 14:26

4 Answers 4

4

You are missing the # sign for the ids, try this:

<script type='text/javascript'>
function generate_page() {
    var x = 0;
    var y = 0;
    var lines = 20;
    var output;
    while (x < lines) {
        while( y < lines*2){
            output = ("<span id='x" + x + "_" + y + "'>x</span>");
            $('#board').prepend(output);
            y++;
        }
        y = 0;
        x++;
        $('#board').append('<br />');
    }
}
</script>

</head>

<body>
<input type='button' value='test' onClick='generate_page()'>
<div id='board'>

</div>

</body>
</html>

also move the button to the body.

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

1 Comment

<input /> is not required in HTML5, and is not valid in HTML4.
1

You're opening a DIV tag and closing a SPAN tag in your JavaScript.

2 Comments

The problem is he forgot an hash #.
Thanks Derek, that was it. :)
1

You are opening a div and closing it with a span, also, you never close the div, and never actually open the span. By the way, utilizing document.createElement("div") is a clean way to do this.

4 Comments

I don't think that's the problem since the browser will auto-correct it.
The browser will close it for you and ignore </span>. But it doesn't work very well all the time.
Oh yeah: document.createElement("div") ==> $("<div>")
lol im old school... i dunno how many times i start typing new XmlHttpRequest(); :P
1

you're using jQuery syntax so you should includ jQuery

like this:

<script src="http://ajax.googleapis.com/ajax/libs/jquery/1/jquery.min.js"></script>
<script type='text/javascript'>
    function generate_page() {
        var x = 0;
        var y = 0;
        var lines = 20;
        var output;
        while (x < lines) {
            while (y < lines * 2) {
                output = ("<div id='x" + x + "_" + y + "'>x</span>");
                $('#board').prepend(output);
                y++;
            }
            y = 0;
            x++;
            $('#board').append('<br />');
        }
    } 
</script></head><body>
<input type='button' value='test' onclick='generate_page()'>
<div id='board'>
</div></body></html>

1 Comment

He did tag it with jquery and he hasnt posted the <html><head> tags to he could have it above.

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.