简体   繁体   中英

Clicking a button and displaying a javascript function on same page

I'm new to html and javascript. How can I display the for loop that I have in my javascript function on the same page upon clicking the button. Right now I click the button and it brings me to a whole different page displaying 0 to -99. I want the numbers to appear below the button on the same page. Any ideas?

         <fieldset>
         <legend>Loop Until -99</legend>
         <form name="loopuntil99" method="post">
         <input type="button" value="Display -99" onclick="displayArray()" />
         </form>
         </fieldset>

         </body>

         <script type="text/javascript">
       function displayArray()
       {

      for (i=0;i>-100;i--)
      {

    document.write(i + ", ");

       } 
        }

    </script>

     </html>

You can add a div and just modify the HTML in that.

    <div id="output"></div>
</body>
<script type="text/javascript">
function displayArray()
{
    var div = document.getElementById("output");
    div.innerHTML = "0";
    for (i=-1;i>-100;i--)
    {
        div.innerHTML += ", " + i;
    } 
}
</script>

Looks like you are outside the <body> tag. Place that code before the </body> tag and try again.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM