<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 ?
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>
These script tags are separate scripts. You can use functions or variables defined in the first one, but you cannot have a for loop spanning both.
The html in between is not in the script either. It looks like your trying to use script tags as if they're <?php?> ones.
Alternatives to document.write, is setting the contents of an element's innerHTML or adding an element to the DOM:
for(i=1;i<10;i++){
var btn = document.createElement("button");
document.body.appendChild(btn);
}
HTML is not a template engine as PHP so you can't separate a code block into multiple script tags. Each code block must be placed entirely in a <script></script> tag pair.
If your purpose is generate 10 buttons with Javascript, please go with document.write option
JavaScript!