...

View Full Version : Creating array from input values.



JohnnyV
01-23-2006, 05: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")

Thanks!

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


<html>
<head>
<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;
}
}

alert(arrs);
}
</script>
</head>
<body>
<form>
<input type=text value=banana>
<input type=text value=pear>
<input type=text value=grapes><br>
<input type="button" value="array" onclick="Fill();">
</form>
</body>
</html>


Good luck;

JohnnyV
01-23-2006, 09:00 PM
yes, perfect!



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum