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 3 of 3
  1. #1
    New Coder
    Join Date
    Oct 2012
    Posts
    18
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Making an Array using a textbox?

    If I wanted a user to type in their 3 favorite colors separated by spaces into a textbox, how could I grab their input and put it into an array?

    Code:
     <script>
    	var runThis = function(){
    		var userInput = document.getElementById('block').value;
    		var thisArray = new Array(userInput);
    		document.getElementById('mySpan').innerHTML = userInput;
    		x = userInput;
    		var thisArray = new Array(x);
    		y = thisArray;
    		return y;
    	}
    
     </script>
    
     <body>
    
    	<input id='block'>
    	<br>
    	<input type='button' onclick='runThis();' value='Click Here'>
    	<br>
    	<span id='mySpan' onclick='test();'> 
    	</span>
     
     </body>

  • #2
    New Coder
    Join Date
    Oct 2012
    Posts
    18
    Thanks
    1
    Thanked 0 Times in 0 Posts
    bump help please!

  • #3
    Supreme Master coder! Old Pedant's Avatar
    Join Date
    Feb 2009
    Posts
    27,147
    Thanks
    80
    Thanked 4,557 Times in 4,521 Posts
    Code:
    <html>
    <body>
    <form id="theForm">
    Enter your three favorite things, separated by spaces:<br/>
        <input name="favorites" style="width: 400px;" />
    </form>
    <hr/>
    You entered: <ol id="output"></ol>
    
    <script type="text/javascript">
    (
      function( )
      {
          var f = document.getElementById("theForm");
          f.favorites.onchange = showMe;
    
          function showMe( )
          {
              var favs = this.value.split(" "); // converts input to an array in one statment
    
              // demo that we got an array:
              var ul = document.getElementById("output");
              for ( var n = 0; n < favs.length; ++n )
              {
                  var li = document.createElement("li");
                  li.innerHTML = favs[n]; // one element of the array
                  ul.appendChild(li);
              }
          }
      }
    )();
    </script>
    </body>
    </html>
    An optimist sees the glass as half full.
    A pessimist sees the glass as half empty.
    A realist drinks it no matter how much there is.


  •  

    Posting Permissions

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