...

View Full Version : Making an Array using a textbox?



codingcodeddode
12-01-2012, 09:45 PM
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?



<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
12-02-2012, 12:07 AM
bump help please!

Old Pedant
12-02-2012, 12:18 AM
<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>



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum