Storing form elements in variables & arrays

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

    #1

    Storing form elements in variables & arrays

    I am trying to wrap my brain around storing form elements inside
    variables & arrays before I move on to a more complicated project. I
    created this simple example to experiment and as far as I can tell, it
    should work but it doesn't. Can someone tell me where I went wrong?

    <html><head>
    <title>Form Test</title>
    <script language="JavaS cript>
    <!--
    function copy(){
    var firstField = new Array();
    var secondField = new Array();

    for(j=0; j<=4; j++){
    firstField[j] = document.myForm .numText;
    }

    for(i=0; i<=4; i++){
    secondField[i] = document.myForm .copyText;
    }

    firstField[0].value = "mark";
    firstField[1].value = "hannon";
    firstField[2].value = "anne";
    firstField[3].value = "mulligan";

    for(k=0; k<=4; k++){
    secondField[k].value = firstField[k].value;
    document.myForm .iValue.value = k;
    }
    }
    // -->
    </script>
    </head>
    <body bgcolor="#FFFFF F">

    <form id="myForm" name="myForm" action="" method="POST"
    enctype="text/plain">

    <input name="numText" type="text" value="" size="15" maxlength="15"> <br>
    <input name="copyText" type="text" value="" size="15" maxlength="15"[color=blue]
    ><br>[/color]
    <input name="iValue" type="text" value="" size="2" maxlength="2" ><br>
    value of i: <input name="copyBut" type="button" value="Copy"
    onClick="copy() ">
    </form>
    </body>
    </html>
  • Michael Winter

    #2
    Re: Storing form elements in variables &amp; arrays

    On Thu, 02 Sep 2004 05:24:33 GMT, Mark Hannon <hannonart@opto nline.net>
    wrote:
    [color=blue]
    > I am trying to wrap my brain around storing form elements inside
    > variables & arrays before I move on to a more complicated project. I
    > created this simple example to experiment and as far as I can tell, it
    > should work but it doesn't.[/color]

    What exactly do you expect the script to do? Its purpose seems unclear to
    me, but it might be exactly what you intended.
    [color=blue]
    > Can someone tell me where I went wrong?[/color]

    You didn't check your HTML. :)

    It always pays to check the validity of your HTML before asking for help,
    especially when scripts are involved. You can use the W3C's validator for
    this:

    <URL:http://validator.w3.or g/>

    There is just one syntax error that has crippled the entire page, but I'll
    suggest several changes.
    [color=blue]
    > <html>[/color]

    Valid HTML pages should contain document type declaration. See:

    <URL:http://www.w3.org/TR/html4/struct/global.html#h-7.2>

    for more information.
    [color=blue]
    > <head>
    > <title>Form Test</title>
    > <script language="JavaS cript>[/color]

    This is the problem. You omitted the closing quote. A good
    syntax-highlighting editor would have shown this. However, whilst we're
    here, you should replace the language attribute with the type attribute.
    The former is deprecated whilst the latter is required.

    <script type="text/javascript">
    [color=blue]
    > <!--[/color]

    Script hiding is obsolete. There are no browsers in use today that will
    render the contents of a script, even if they no ability to execute
    scripts. If you really are worried about content rendering, move the
    script into an external file and use the src attribute. This should be
    done for production sites, anyway.
    [color=blue]
    > function copy(){
    > var firstField = new Array();
    > var secondField = new Array();[/color]

    It tends to be simpler to use array literal for declaring arrays:

    var firstField = [],
    secondField = [];

    If you want to initialise arrays with this syntax, you simply place the
    values within the brackets separated by spaces.

    anArray = ['my', 'initialised', 'array', 2004];
    [color=blue]
    > for(j=0; j<=4; j++){
    > firstField[j] = document.myForm .numText;
    > }[/color]

    Just as you declared the first- and secondField variables, you should do
    the same for 'j', above. Using the var keyword prevents variables from
    becoming global.

    for(var j = 0; j <= 4; ++j) {
    // ...
    }

    The same applies to the other two counters used. However, as you restart
    each counter from zero, you might as well use the one variable.

    Another improvement can be used when you access the same property
    repeatedly.

    var form = document.forms['myForm'];

    This means that from now on, you can use the form identifier to access the
    form object.

    More information about this, and the property accessor I introduced above,
    can be found in the group FAQ (and the accompanying notes):

    <URL:http://jibbering.com/faq/#FAQ4_13>

    [snip]
    [color=blue]
    > // -->[/color]

    Be sure to remove this with the other comment delimiter.
    [color=blue]
    > </script>
    > </head>
    > <body bgcolor="#FFFFF F">[/color]

    The bgcolor attribute is deprecated. All new HTML documents should really
    be using the Strict DTD and CSS instead of presentational attributes and
    elements.

    [snip]

    If you have other problems, remember to do two things:

    1) Check your HTML
    2) Read the FAQ

    One of those might solve the issue without even having to ask for help.

    Hope that helps,
    Mike

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

    Comment

    • Mark Hannon

      #3
      Re: Storing form elements in variables &amp; arrays

      Thanks for the help Mike,

      It was late when I posted that message and I had been working on this
      and tweaking it for a few hours. As is often the case, the longer you
      stare at and labor over something the less you are able to see the
      problems you have created for yourself. I'll take your advice on the
      validation, array declaration and JavaScript tags.

      Mark

      *** Sent via Developersdex http://www.developersdex.com ***
      Don't just participate in USENET...get rewarded for it!

      Comment

      Working...