View Full Version : Creating array from input values.

01-23-2006, 04:59 PM
How would I go about creating a new arrary which consists of the values for every text input within a form? The array would need to be created dynamically when the function is called (by clicking a button or whatever). Is this possible?

So if the input fields looked like this:

<input type=text value=banana>
<input type=text value=pear>
<input type=text value=grapes>

An array like this would be created once i ran the function:
var values=new Array("banana",pear","grapes")


01-23-2006, 05:44 PM
Something like this?

<script type="text/javascript">
function Fill()
var arrs = [];
var txts = document.getElementsByTagName('input');
for(var i = 0; i < txts.length; i++)
if(txts[i].type == 'text')
arrs[arrs.length] = txts[i].value;

<input type=text value=banana>
<input type=text value=pear>
<input type=text value=grapes><br>
<input type="button" value="array" onclick="Fill();">

Good luck;

01-23-2006, 08:00 PM
yes, perfect!