Go Back   CodingForums.com > :: Client side development > JavaScript programming

Before you post, read our: Rules & Posting Guidelines

Reply
 
Thread Tools Rate Thread
Enjoy an ad free experience by logging in. Not a member yet? Register.
Old 12-01-2012, 08:45 PM   PM User | #1
codingcodeddode
New Coder

 
Join Date: Oct 2012
Posts: 18
Thanks: 1
Thanked 0 Times in 0 Posts
codingcodeddode is an unknown quantity at this point
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>
codingcodeddode is offline   Reply With Quote
Old 12-01-2012, 11:07 PM   PM User | #2
codingcodeddode
New Coder

 
Join Date: Oct 2012
Posts: 18
Thanks: 1
Thanked 0 Times in 0 Posts
codingcodeddode is an unknown quantity at this point
bump help please!
codingcodeddode is offline   Reply With Quote
Old 12-01-2012, 11:18 PM   PM User | #3
Old Pedant
Supreme Master coder!

 
Old Pedant's Avatar
 
Join Date: Feb 2009
Posts: 23,203
Thanks: 59
Thanked 3,996 Times in 3,965 Posts
Old Pedant is a name known to allOld Pedant is a name known to allOld Pedant is a name known to allOld Pedant is a name known to allOld Pedant is a name known to allOld Pedant is a name known to all
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.
Old Pedant is online now   Reply With Quote
Reply

Bookmarks

Jump To Top of Thread


Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT +1. The time now is 09:36 PM.


Advertisement
Log in to turn off these ads.