Help: Problem with accessing form data using javascript.

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Ant

    #1

    Help: Problem with accessing form data using javascript.

    Hi,

    I'm, having some problems with this function.

    function displayElements ()
    {
    for (i=0;i<document .forms[0].elements.lengt h; ++i)
    {
    document.writel n(document.form s[0].elements[i].value);
    }
    }

    I'm trying to loop through the only form (form[0]?) on my webpage and
    display all their values. For some reason I'm only being shown the first
    value?
    I'm not sure what I'm doing wrong, any help much appreciated.
    Thanks


  • McKirahan

    #2
    Re: Problem with accessing form data using javascript.

    "Ant" <asdf@asdf.co m> wrote in message
    news:ct5o8c$pch $1@wisteria.csv .warwick.ac.uk. ..[color=blue]
    > Hi,
    >
    > I'm, having some problems with this function.
    >
    > function displayElements ()
    > {
    > for (i=0;i<document .forms[0].elements.lengt h; ++i)
    > {
    > document.writel n(document.form s[0].elements[i].value);
    > }
    > }
    >
    > I'm trying to loop through the only form (form[0]?) on my webpage and
    > display all their values. For some reason I'm only being shown the first
    > value?
    > I'm not sure what I'm doing wrong, any help much appreciated.
    > Thanks[/color]

    i++


    Comment

    • Michael Winter

      #3
      Re: Problem with accessing form data using javascript.

      On Tue, 25 Jan 2005 10:01:36 -0600, McKirahan <News@McKirahan .com> wrote:
      [color=blue]
      > "Ant" <asdf@asdf.co m> wrote in message
      > news:ct5o8c$pch $1@wisteria.csv .warwick.ac.uk. ..[/color]
      [color=blue][color=green]
      >> I'm, having some problems with this function.[/color][/color]

      When posting code, please indent it (preferably using two spaces). It's
      much easier to read that way.
      [color=blue][color=green]
      >> function displayElements ()
      >> {
      >> for (i=0;i<document .forms[0].elements.lengt h; ++i)[/color][/color]

      The variable, i, should be declared using the var keyword otherwise it
      will become global. It would also be more efficient to save a reference to
      the elements collection rather than resolving it twice on every loop
      iteration:

      var e = document.forms[0].elements;
      for(var i = 0, n = e.length; i < n; ++i) {
      /* ... */
      }
      [color=blue][color=green]
      >> document.writel n(document.form s[0].elements[i].value);[/color][/color]

      When you call the write (or writeln) method, the argument is converted to
      a string and written to the document stream. However, if the stream is
      closed (which happens once the document has finished loading or the
      document.close method is called), the stream is re-opened. This causes the
      document, and everything in it (including your scripts and forms) to be
      deleted. That's why the loop only runs once.

      The write (or writeln) method should generally be reserved for dynamically
      writing content as a document loads.

      For this sort of thing, I would create a TEXTAREA element at the end of
      the document, give it an id, and dump your output into that:

      function displayElements () {
      var e = document.forms[0].elements,
      o = document.getEle mentById('outpu t');

      for(var i = 0, n = e.length; i < n; ++i) {
      o.value += e[i].value + '\n';
      }
      }


      <textarea id="output" rows="10" cols="40"></textarea>

      [snip]
      [color=blue]
      > i++[/color]

      Why would that help? Both pre- and post-fix increment (and decrement) are
      perfectly valid. Out of habit (more than anything else), I always use
      prefix variety unless I specifically want the semantics of the latter.

      Mike

      --
      Michael Winter
      Replace ".invalid" with ".uk" to reply by e-mail.

      Comment

      • McKirahan

        #4
        Re: Problem with accessing form data using javascript.

        "McKirahan" <News@McKirahan .com> wrote in message
        news:ztGdnSbOsq ae8GvcRVn-vA@comcast.com. ..[color=blue]
        > "Ant" <asdf@asdf.co m> wrote in message
        > news:ct5o8c$pch $1@wisteria.csv .warwick.ac.uk. ..[color=green]
        > > Hi,
        > >
        > > I'm, having some problems with this function.
        > >
        > > function displayElements ()
        > > {
        > > for (i=0;i<document .forms[0].elements.lengt h; ++i)
        > > {
        > > document.writel n(document.form s[0].elements[i].value);
        > > }
        > > }
        > >
        > > I'm trying to loop through the only form (form[0]?) on my webpage and
        > > display all their values. For some reason I'm only being shown the[/color][/color]
        first[color=blue][color=green]
        > > value?
        > > I'm not sure what I'm doing wrong, any help much appreciated.
        > > Thanks[/color]
        >
        > i++[/color]

        Try this:

        function displayElements () {
        var frm = document.forms[0];
        var max = frm.elements.le ngth;
        var out = "";
        for (var i=0; i<max; i++) {
        try {
        out += "<br>" + frm.elements[i].value;
        } catch (err) { }
        }
        document.write( out);
        }


        Comment

        • Ant

          #5
          Re: Problem with accessing form data using javascript.

          Thankyou both very much for your replies. They've been a great help!

          Cheers


          Comment

          • RobG

            #6
            Re: Problem with accessing form data using javascript.

            Michael Winter wrote:
            [...][color=blue]
            > When you call the write (or writeln) method, the argument is converted
            > to a string and written to the document stream. However, if the stream
            > is closed (which happens once the document has finished loading or the
            > document.close method is called), the stream is re-opened. This causes
            > the document, and everything in it (including your scripts and forms)
            > to be deleted.[/color]

            Whilst that is what the spec says *should* happen, and it does as far
            as I can tell in IE and Firefox (and likely Mozilla and Netscape),
            scripts seem to linger in Safari and OmniWeb.

            The point being that for some browsers, scripts are not replaced (or at
            least, not entirely). I guess this is a bug or at best an
            inconsistency with the HTML specification, but developers should not
            expect that all contents are removed just by re-opening the document
            and writing to it.

            For example:

            <html><head><ti tle>play</title>
            </head><body>
            <script type="text/javascript">
            function firstFunction() {
            var buttonType='<in put type="button" value="Proceed" name="contOn" '
            + ' onclick="second Function();">'
            document.write( buttonType);
            document.close( );
            }

            function secondFunction( ) {
            alert('I am the second function');
            }
            </script>
            <p>Here is some text</p>
            <button onclick="firstF unction();">fir stFunction</button>
            <p>Here is some more text</p>
            </body</html>

            When the first button is clicked, it writes a new button to the page
            with an onclick that calls secondFunction. secondFunction should have
            been deleted from the document, but it runs. Now this may be because
            the script is held in memory (or for some other reason you may guess
            at) but the point is it's still "there" in Safari.

            Having said that, is there a more explicit way of emptying the
            document contents? Perhaps by removing the HTML element?

            --
            Rob

            Comment

            • Randy Webb

              #7
              Re: Help: Problem with accessing form data using javascript.

              Ant wrote:
              [color=blue]
              > Hi,
              >
              > I'm, having some problems with this function.
              >
              > function displayElements ()
              > {
              > for (i=0;i<document .forms[0].elements.lengt h; ++i)
              > {
              > document.writel n(document.form s[0].elements[i].value);
              > }
              > }
              >
              > I'm trying to loop through the only form (form[0]?) on my webpage and
              > display all their values. For some reason I'm only being shown the first
              > value?
              > I'm not sure what I'm doing wrong, any help much appreciated.[/color]

              You are using document.write after the page is finished loading. What
              that does is totally destroy the current page and replace it. Along with
              that page your script is gone. It finds the first element,
              document.write' s it, and then it's finished because there is no script
              left. If you want to print them all out, concatenate a variable and then
              print that variable:

              function displayElements ()
              {
              var myVar = '';
              for (i=0;i<document .forms[0].elements.lengt h; ++i)
              {
              myVar =+ document.forms[0].elements[i].value;
              }
              document.write( myVar)
              }

              Or consult the group FAQ for DynWrite and dump it to a DIV element in
              the page.

              --
              Randy
              comp.lang.javas cript FAQ - http://jibbering.com/faq

              Comment

              • Grant Wagner

                #8
                Re: Problem with accessing form data using javascript.

                "RobG" <rgqld@iinet.ne t.auau> wrote in message
                news:41f67801$0 $10564$5a62ac22 @per-qv1-newsreader-01.iinet.net.au ...[color=blue]
                > Michael Winter wrote:
                > [...][color=green]
                >> When you call the write (or writeln) method, the argument is
                >> converted to a string and written to the document stream. However,
                >> if the stream is closed (which happens once the document has
                >> finished loading or the document.close method is called), the stream
                >> is re-opened. This causes the document, and everything in it
                >> (including your scripts and forms) to be deleted.[/color]
                >
                > Whilst that is what the spec says *should* happen, and it does as far
                > as I can tell in IE and Firefox (and likely Mozilla and Netscape),
                > scripts seem to linger in Safari and OmniWeb.
                >
                > The point being that for some browsers, scripts are not replaced (or
                > at
                > least, not entirely). I guess this is a bug or at best an
                > inconsistency with the HTML specification, but developers should not
                > expect that all contents are removed just by re-opening the document
                > and writing to it.[/color]

                I could not disagree more strongly. Developers should assume without
                exception that issuing document.open() , document.write( ) (which
                implicitly calls document.open() ), location.href = value, Form#submit()
                or any other action that causes the browser to navigate or re-write the
                main window content will cause the current document and all script
                contained in the current window to be immediately unavailable. Your
                example demonstrated behaviour which authors of client-side code should
                not even attempt to rely on.

                I even regard: <body onunload="alert ('Leaving...'); "> to be "incorrect"
                (although it appears to work in IE 6.0.2900 and Firefox 1.0).

                The onunload event fires when the page is unloaded. The code that is
                attached to that event is (or in my mind should be) destroyed but the
                very event which triggers it.

                How would I use onunload then? Well, I wouldn't. But if I did, I'd have
                code in another frame, or window, which executes when the event
                triggers. Of course, this still leaves the tricky problem that the call
                to the code in the other frame or window is wrapped in an anonymous
                function in the destroyed window, but I'll overlook that.

                --
                Grant Wagner <gwagner@agrico reunited.com>
                comp.lang.javas cript FAQ - http://jibbering.com/faq


                Comment

                Working...