Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 5 of 5
  1. #1
    New Coder
    Join Date
    Apr 2018
    Location
    Denmark
    Posts
    35
    Thanks
    19
    Thanked 0 Times in 0 Posts

    HTML/JS Input question?

    A question. How do I create a submit button with number input box on HTML and calling to input a number into JS?
    Like this in HTML.. Play the Game [number input box] [start new game button]

    Basically, I want to create an input text box (Input number of Bots) and onClick button (Start new game) to choose how many bots to play in the game, i.e 10, 100, 1000 ?

    Using this line.. for ( var i = 0 ; i < 10 ; i++ ) {

    for ( var i = 0 ; i < (change the number here from input) ; i++ ) {
    i < 10 or i < 100 or i < 100 .. etc

  2. #2
    Master Coder sunfighter's Avatar
    Join Date
    Jan 2011
    Location
    Washington
    Posts
    7,613
    Thanks
    34
    Thanked 1,029 Times in 1,026 Posts
    Answer to question, but not with a for loop:
    Code:
    <body>
     
      Howmanybots?  <input  type="number" id="bots"><br>
      <button id="submitButton">Play Game</button>
    
      <script>
     document.getElementById("submitButton").addEventListener("click", function(){
       var test = document.getElementById("bots").value;
       alert(test);   //To  show  the value of  the input
     });
      </script>
    </body>
    Evolution - The non-random survival of random variants.
    Physics is actually atoms trying to understand themselves.

  3. Users who have thanked sunfighter for this post:

    blazestarfall (Jun 8th, 2018)

  4. #3
    New Coder
    Join Date
    Apr 2018
    Location
    Denmark
    Posts
    35
    Thanks
    19
    Thanked 0 Times in 0 Posts
    How do I use this code? It works when I do the input initially with alert(test);
    then I placed "test" in the i < test ;
    Doesn't work

    Code:
    	 document.getElementById("submitButton").addEventListener("click", function(){
             var test = document.getElementById("bots").value;
                     // alert(test);   //To  show  the value of  the input
             }};
    		 
    		for ( var i = 0 ; i < test ; i++ ) {	
    
                        // code content here
    
    		}

  5. #4
    Master Coder sunfighter's Avatar
    Join Date
    Jan 2011
    Location
    Washington
    Posts
    7,613
    Thanks
    34
    Thanked 1,029 Times in 1,026 Posts
    Code:
    <script>
       document.getElementById("submitButton").addEventListener("click", function(){
        var test = document.getElementById("bots").value;
        alert(test);   //To  show  the value of  the input
    
        for ( var i = 0 ; i < test ; i++ ) {	
          alert(i);
        }
      });
    </script>
    Don't know where you placed your loop, but put it in the listener function.
    Evolution - The non-random survival of random variants.
    Physics is actually atoms trying to understand themselves.

  6. Users who have thanked sunfighter for this post:

    blazestarfall (Jun 8th, 2018)

  7. #5
    New Coder
    Join Date
    Apr 2018
    Location
    Denmark
    Posts
    35
    Thanks
    19
    Thanked 0 Times in 0 Posts
    will try to figure it out. Thanks
    Last edited by blazestarfall; Jun 9th, 2018 at 12:28 AM.


 

Tags for this Thread

Posting Permissions

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