f



javascript and []

<form name=f>
<input type=text name="apple[]" value=1>
</form>

<script>
document.f.apple[].value=2
</script>

///////////////////////////
this generates an script error in IE
because of apple[]
so how do I get around the error?
(must use "apple[]" for server cgi)

thanks in advance :)




0
apple
7/10/2004 11:44:05 PM
comp.lang.javascript 38370 articles. 2 followers. javascript4 (1315) is leader. Post Follow

5 Replies
796 Views

Similar Articles

[PageSpeed] 59

document.getElementsByName("apple[]") //returns an object array

-- 
www.vicdir.com
<apple@apple.com> д���ʼ� news:ccpv0f$440$3@news.seed.net.tw...
> <form name=f>
> <input type=text name="apple[]" value=1>
> </form>
>
> <script>
> document.f.apple[].value=2
> </script>
>
> ///////////////////////////
> this generates an script error in IE
> because of apple[]
> so how do I get around the error?
> (must use "apple[]" for server cgi)
>
> thanks in advance :)
>
>
>
>


0
CryingClinton
7/11/2004 12:53:51 AM
 wrote on 11 jul 2004 in comp.lang.javascript:
> <form name=f>
> <input type=text name="apple[]" value=1>
> </form>
> 
> <script>
> document.f.apple[].value=2
> </script>
> 

<form name=f>
<input type=text name="apple[]" value=1>
</form>

<script type="text/javascript">
document.f[0].value=2 // the first child of f
</script>

========= or ==========

<form name=f>
<input type=text name="apple[]" id=appl value=1>
</form>

<script type="text/javascript">
document.getElementById('appl').value=2
</script>


-- 
Evertjan.
The Netherlands.
(Please change the x'es to dots in my emailaddress)
0
Evertjan
7/11/2004 8:46:17 AM
apple@apple.com wrote:
> so how do I get around the error?

document.forms['f'].elements['apple[]'].value = 2;

> (must use "apple[]" for server cgi)
>

Not really. I assume that you are using php, which also supports an
always_populate_raw_post_data directive.

With this directive enbled, you can simply use normal names and still use
arrays:

<form method="post" action="...">
   <input type="checkbox" name="fruit" value="apple" /> Apple<br />
   <input type="checkbox" name="fruit" value="banana" /> Banana<br />
   <input type="checkbox" name="fruit" value="pear" /> Pear<br />
   <input type="submit" />
</form>
.....
 <?
      if (!isset($HTTP_RAW_POST_DATA) ||
          !strstr($HTTP_RAW_POST_DATA, 'fruit=')) {
          print "Please select some fruits";
      } else {
           parse_str(
                str_replace(
                     "fruit=",
                     "fruit[]=",
                     $HTTP_RAW_POST_DATA
                ),
                $fruits
           );
           print "Selected fruits:<br />";
           foreach ($fruits['fruit'] as $fruit) {
                print "$fruit<br />";
           }
      }
 ?>


JW




0
Janwillem
7/11/2004 11:06:07 AM
"Evertjan." wrote:

>  wrote on 11 jul 2004 in comp.lang.javascript:
> > <form name=f>
> > <input type=text name="apple[]" value=1>
> > </form>
> >
> > <script>
> > document.f.apple[].value=2
> > </script>
> >
>
> <form name=f>
> <input type=text name="apple[]" value=1>
> </form>
>
> <script type="text/javascript">
> document.f[0].value=2 // the first child of f
> </script>
>
> ========= or ==========
>
> <form name=f>
> <input type=text name="apple[]" id=appl value=1>
> </form>
>
> <script type="text/javascript">
> document.getElementById('appl').value=2
> </script>

document.forms['yourForm'].elements['apple[]'].value = 2;

Now you don't need to remember form input indices or use a
browser that is completely W3C compliant.

This is covered at <url: http://jibbering.com/faq/#FAQ4_25 />.

--
Grant Wagner <gwagner@agricoreunited.com>
comp.lang.javascript FAQ - http://jibbering.com/faq


0
Grant
7/13/2004 4:37:47 PM
"Janwillem Borleffs" <jw@jwscripts.com> wrote in message news:<40f11f24$0$1746$abc4f4c3@news.wanadoo.nl>...
> apple@apple.com wrote:
> > so how do I get around the error?
> 
> document.forms['f'].elements['apple[]'].value = 2;
> 
> > (must use "apple[]" for server cgi)
> >
> 
> Not really. I assume that you are using php, which also supports an
> always_populate_raw_post_data directive.
> 
> With this directive enbled, you can simply use normal names and still use
> arrays:

[snip]
That would depend on if the compilation of your version of PHP
supports ALWAYS_POPULATE_RAW_POST_DATA.. you're safer using an array
construct, sorry.

<input type="checkbox" name="fruit[]" value="apple">Apple

in Javascript you would call this element cross-browser:
document.forms['myForm'].elements['fruit[]']

Phil

> 
> <form method="post" action="...">
>    <input type="checkbox" name="fruit" value="apple" /> Apple<br />
>    <input type="checkbox" name="fruit" value="banana" /> Banana<br />
>    <input type="checkbox" name="fruit" value="pear" /> Pear<br />
>    <input type="submit" />
> </form>
> ....
>  <?
>       if (!isset($HTTP_RAW_POST_DATA) ||
>           !strstr($HTTP_RAW_POST_DATA, 'fruit=')) {
>           print "Please select some fruits";
>       } else {
>            parse_str(
>                 str_replace(
>                      "fruit=",
>                      "fruit[]=",
>                      $HTTP_RAW_POST_DATA
>                 ),
>                 $fruits
>            );
>            print "Selected fruits:<br />";
>            foreach ($fruits['fruit'] as $fruit) {
>                 print "$fruit<br />";
>            }
>       }
>  ?>
> 
> 
> JW
0
soazine
7/19/2004 2:04:24 PM
Reply: