1
<script>
for(var numCols = 1; numCols <= 12; numCols++){
    document.write(
                        "<div class='row'>" +
                        "<div class='well text-center col-lg-" + [numCols] + "'>" + ".col-lg-" + [numCols] + "</div>" +
                        "</div>"; 
                    );
}
</script>

I know there's document.write/document.writeln but I have this script and I don't know what's wrong. I'm trying to visualize Bootstrap's col-md-* thing

Pete
  • 57,112
  • 28
  • 117
  • 166
mincedMinx
  • 159
  • 1
  • 3
  • 12
  • Possible duplicate of [Adding HTML elements with JavaScript](http://stackoverflow.com/questions/3425142/adding-html-elements-with-javascript) – thanksd Sep 22 '16 at 14:34
  • 1
    Take a look at: [When should one use .innerHTML and when document.write in JavaScript](http://stackoverflow.com/questions/10085109/when-should-one-use-innerhtml-and-when-document-write-in-javascript) and [document.write vs appendChild](http://stackoverflow.com/questions/22172589/document-write-vs-appendchild) – t.niese Sep 22 '16 at 14:34

5 Answers5

3

Remove the semicolon after last </div>

<script>
for(var numCols = 1; numCols <= 12; numCols++){
    document.write(
                        "<div class='row'>" +
                        "<div class='well text-center col-lg-" + [numCols] + "'>" + ".col-lg-" + [numCols] + "</div>" +
                        "</div>"
                    );
}
</script>
gurvinder372
  • 66,980
  • 10
  • 72
  • 94
3

The problem is with the [numCols] part of string concatenation and unnecessary semicolon at the end of function call argument.

Basically, what this "part" does is it returns an Array with one element, resolving to numCols variable value. You want to return the variable itself, so just remove [] wrapper. Technically, JS allows you to use this syntax, because it casts the array to string, concatenating all the elements within the array with an empty string (equivalent to [ numCols ].join("")), but it's just unnecessary in this case.

Another thing - you are terminating HTML string with "</div>"; - the semicolon is invalid in this place, you should remove it.

mdziekon
  • 3,531
  • 3
  • 22
  • 32
3

Remove the semiColon inside your document.write method.

for(var numCols = 1; numCols <= 12; numCols++){
    document.write(
                        "<div class='row'>" +
                        "<div class='well text-center col-lg-" + [numCols] + "'>" + ".col-lg-" + [numCols] + "</div>" +
                        "</div>"
                    );
}
Kevin Grosgojat
  • 1,386
  • 8
  • 13
1

Be aware. Working with the DOM means, the client has to do the work. Doing several changes to the DOM could lead us to several performance issues.

The ideal is, do not modify the DOM. If you really need to do this. Find the way to do the less changes to the DOM.

The piece of code you provide us have the possibility to be improved.

In your code, you are using a loop. 12 times the DOM would change. These is far away of be a good practice.

A better approach is to write the DOM only 1 time. In this scenario you can easily achieve adding two lines of code.

From this (12 DOM changes):

for(var numCols = 1; numCols <= 12; numCols++){
    document.write(
        "<div class='row'>" +
        "<div class='well text-center col-lg-" + [numCols] + "'>" + ".col-lg-" + [numCols] + "</div>" +
        "</div>"
    );
}

To this (1 change to the DOM):

var newHtml = '';
for(var numCols = 1; numCols <= 12; numCols++){
    newHtml+= "<div class='row'>" +
        "<div class='well text-center col-lg-" + [numCols] + "'>" + ".col-lg-" + [numCols] + "</div>" +
        "</div>";
}
document.write(newHtml);
manuerumx
  • 1,230
  • 14
  • 28
0

You can use innerHTML to change the content of DOM element when the page is loaded.

window.onload = function() {
    var content = "";
    for(var numCols = 1; numCols <= 12; numCols++) {
        content += "<div class='row'>" +
                   "<div class='well text-center col-lg-" + numCols + "'>" +              ".col-lg-" + numCols + "</div>" +
                   "</div>";
    }

    document.getElementsByTagName('body')[0].innerHTML = content;
}
Md. Salahuddin
  • 1,062
  • 15
  • 22