Soham Pandya Soham Pandya - 7 months ago 7
HTML Question

write html in javascript without documet.write

<script >

for(i=0;i<=9;i++)
{

</script>

<input type="button" value="1"/>

<script type="text/javascript">

}

</script>


This puts button only 1 time if I use document.write it prints 10 times why ?

Answer

JavaScript does not behave in the same way PHP does. Whatever you place between the <script> tags is a script in and of itself.

So you have two separate scripts: This:

for(i=0;i<=9;i++)
{

and this one:

}

Imagine what would happen if you placed these two scripts into two separate files? That's right, both would fail because of syntax errors. If you take a look at the console you'll see what errors I'm talking about.

If you want to print 10 buttons do something like this:

<div id="mainDiv">

</div>

<script>
    var mainDiv = document.getElementById('mainDiv');
    for(var i=0; i<10; i++){
       mainDiv.innerHtml += "<input type='button' value='1'>";
    }
</script>
Comments