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
    Sep 2005
    Posts
    25
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Creating array from input values.

    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!

  • #2
    teh Moderatorinator
    Join Date
    Sep 2004
    Location
    USA
    Posts
    2,472
    Thanks
    4
    Thanked 40 Times in 40 Posts
    Something like this?
    Code:
    <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;

  • #3
    New Coder
    Join Date
    Sep 2005
    Posts
    25
    Thanks
    0
    Thanked 0 Times in 0 Posts
    yes, perfect!


  •  

    Posting Permissions

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