javascript and []

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Guest's Avatar

    javascript and []

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

    <script>
    document.f.appl e[].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 :)




  • CryingClinton

    #2
    Re: javascript and []

    document.getEle mentsByName("ap ple[]") //returns an object array

    --

    <apple@apple.co m> дÈëÓʼþ news:ccpv0f$440 $3@news.seed.ne t.tw...[color=blue]
    > <form name=f>
    > <input type=text name="apple[]" value=1>
    > </form>
    >
    > <script>
    > document.f.appl e[].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 :)
    >
    >
    >
    >[/color]


    Comment

    • Evertjan.

      #3
      Re: javascript and []

      wrote on 11 jul 2004 in comp.lang.javas cript:[color=blue]
      > <form name=f>
      > <input type=text name="apple[]" value=1>
      > </form>
      >
      > <script>
      > document.f.appl e[].value=2
      > </script>
      >[/color]

      <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.getEle mentById('appl' ).value=2
      </script>


      --
      Evertjan.
      The Netherlands.
      (Please change the x'es to dots in my emailaddress)

      Comment

      • Janwillem Borleffs

        #4
        Re: javascript and []

        apple@apple.com wrote:[color=blue]
        > so how do I get around the error?[/color]

        document.forms['f'].elements['apple[]'].value = 2;
        [color=blue]
        > (must use "apple[]" for server cgi)
        >[/color]

        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_R AW_POST_DATA) ||
        !strstr($HTTP_R AW_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




        Comment

        • Grant Wagner

          #5
          Re: javascript and []

          "Evertjan." wrote:
          [color=blue]
          > wrote on 11 jul 2004 in comp.lang.javas cript:[color=green]
          > > <form name=f>
          > > <input type=text name="apple[]" value=1>
          > > </form>
          > >
          > > <script>
          > > document.f.appl e[].value=2
          > > </script>
          > >[/color]
          >
          > <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.getEle mentById('appl' ).value=2
          > </script>[/color]

          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@agrico reunited.com>
          comp.lang.javas cript FAQ - http://jibbering.com/faq


          Comment

          • Phil Powell

            #6
            Re: javascript and []

            "Janwillem Borleffs" <jw@jwscripts.c om> wrote in message news:<40f11f24$ 0$1746$abc4f4c3 @news.wanadoo.n l>...[color=blue]
            > apple@apple.com wrote:[color=green]
            > > so how do I get around the error?[/color]
            >
            > document.forms['f'].elements['apple[]'].value = 2;
            >[color=green]
            > > (must use "apple[]" for server cgi)
            > >[/color]
            >
            > 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:[/color]

            [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">A pple

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

            Phil
            [color=blue]
            >
            > <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_R AW_POST_DATA) ||
            > !strstr($HTTP_R AW_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[/color]

            Comment

            Working...