1
    <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 ?

3
  • Because the button was placed inside a loop. Commented Apr 28, 2016 at 10:07
  • Check console for errors... And start with the basics of JavaScript! Commented Apr 28, 2016 at 10:08
  • i want to add button 10 times if i will put tag like this in php it will add button 10 times but in javascript i need to put document.write want to know why Commented Apr 28, 2016 at 10:10

4 Answers 4

1

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>
Sign up to request clarification or add additional context in comments.

Comments

1

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);
}

2 Comments

i want to add button 10 times if i will put tag like this in php it will add button 10 times but in javascript i need to put document.write want to know why
Yes, I figured you were confusing it with php. There is no why, javascript just doesn't work that way. The best solution is add it to the DOM.
0

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

2 Comments

Do you have any other restrictions with this option?
Of course there are many other options, but in this context, I assume that he just want to know how 2 options work :)
0

you are looking for some sort of server side templating. remember javascript runs on the client.

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.