www.webdeveloper.com
Results 1 to 6 of 6

Thread: send a form array to js function

  1. #1
    Join Date
    Apr 2005
    Posts
    210

    Unhappy send a form array to js function

    Code:
    <script type="text/javascript">
    function myFunction(form)
    {
    	alert(form.my[0].value);
    }
    </script>
    
    <style>
    #the-form
    {
    	border: 2px solid #FF9900; 
    	padding: 20px; 
    	display: inline-block;
    }
    </style>
    
    <form id="the-form" name="myform">
    	<input type='text' value="eeee" name="my[]" />
    	<input type='text' value="ffff" name="my[]" />
    </form>
    <br>
    <input type="button" value="Show" onclick="myFunction(myform);" />
    Only error above is the bold line in js function myFunction(form).

    If not use document.getElementsByTagName('input') to enum all inputs,
    how does a js function read form array directly as pseudo-code above?

    Thanks.

    .

  2. #2
    Join Date
    May 2006
    Location
    Somewhere behind your screen
    Posts
    1,674
    may be this way?

    Code:
    <!DOCTYPE html>
    <html>
    <head>
    <meta http-equiv="content-type" content="text/html; charset=utf-8" />
    <title>...</title>
    <head>
    <style>
    #the-form
    {
    	border: 2px solid #FF9900; 
    	padding: 20px; 
    	display: inline-block;
    }
    </style>
    <script type="text/javascript">
    function myFunction(form){
    alert(form[0].value+'\n'+form[1].value);
    }
    </script>
    </head>
    <body>
    <form id="the-form" name="myform">
    	<input type='text' value="eeee" name="my[]" />
    	<input type='text' value="ffff" name="my[]" />
    
    <br>
    <input type="button" value="Show" onclick="myFunction(this.form);" />
    </form>
    </body>
    </html>
    Last edited by Padonak; 05-15-2012 at 03:38 PM. Reason: new code

  3. #3
    Join Date
    May 2012
    Posts
    14
    You need not give a my[] name to the input elements. by default all elements coming under a form is an array. Please the the following (changes highlighted in green bold)


    <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
    "http://www.w3.org/TR/html4/loose.dtd">
    <html xmlns="http://www.w3.org/1999/xhtml">
    <head>
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
    <title>New Web Project</title>
    <style>
    #the-form
    {
    border: 2px solid #FF9900;
    padding: 20px;
    display: inline-block;
    }
    </style>

    <script type="text/javascript">
    function myFunction(form)
    {
    alert(form)
    alert(form[0].value);
    }
    </script>
    </head>
    <body>
    <form id="the-form" name="myform">
    <input type='text' value="eeee" [COLOR="rgb(46, 139, 87)"]name="my1"[/COLOR] />
    <input type='text' value="ffff" [COLOR="rgb(46, 139, 87)"]name="my2"[/COLOR] />
    </form>
    <br>
    <input type="button" value="Show" onclick="myFunction(myform);" />
    </body>
    </html>

  4. #4
    Join Date
    May 2012
    Posts
    14
    Please ignore the color codes around the input elements. It should be like the following

    <form id="the-form" name="myform">
    <input type='text' value="eeee" name="my1" />
    <input type='text' value="ffff" name="my2" />
    </form>

  5. #5
    Join Date
    Apr 2005
    Posts
    210
    Thank for above.

    form[0] and form[1] work fine.
    I posted another post, that is my final purpose.

    http://www.webdeveloper.com/forum/sh...80#post1207380

    Please have a look.

    .

  6. #6
    Join Date
    May 2012
    Posts
    14
    I have responded to your othe post

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Posting Permissions

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



Recent Articles