www.webdeveloper.com
Results 1 to 2 of 2

Thread: Javascript Loop

  1. #1
    Join Date
    May 2009
    Posts
    1

    Javascript Loop

    I have make a loop it loops 1 to 10 but it not loop 2 to 10
    here is the code
    please help

    <body>

    <script type="text/javascript">

    function loop(T1,T2)
    {
    var i=T1.value;
    var j=T2.value;
    var x=0;
    for (x=T1.value;x<=T2.value;x++)
    {
    document.write(x);
    document.write("<br>");
    }
    }
    </script>



    <form>

    <label>
    <input name="T1" type="text" id="T1" />
    </label>
    <label>
    <input name="T2" type="text" id="T2" />
    </label>
    <input type="button" value="Click me!" onclick="loop(T1,T2)" />
    </form>
    </body>
    </html>

  2. #2
    Join Date
    Dec 2005
    Location
    FL
    Posts
    7,257

    Lightbulb Try this ...

    You cannot use document.write to change a page once it has been loaded.
    Doing so, or at least trying, causes the program to reset and start over.

    For proof, try this variation:
    Code:
    <html>
    <head>
    <title>Test</title>
    
    <script type="text/javascript">
    // For: http://www.webdeveloper.com/forum/showthread.php?t=209152
    
    function loop() {
      var i=document.getElementById('T1').value*1;
      var g=document.getElementById('T2').value*1;
      var str = '';
      for (var x=i; x<=g; x++) { str += x+'<br>'; }
      document.getElementById('divStr').innerHTML = str;
    }
    </script>
    
    <body>
    <label> <input name="T1" type="text" id="T1" /> </label>
    <label> <input name="T2" type="text" id="T2" /> </label>
    <input type="button" value="Click me!" onclick="loop()" />
    <div id="divStr"></div>
    </body>
    </html>
    Plus you had some invalid syntax.
    Check the error console in FF with you code for some clues.


Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
HTML5 Development Center



Recent Articles