Storing form elements in variables & arrays

M

Mark Hannon

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="JavaScript>
<!--
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 = 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="#FFFFFF">

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

<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>
 
M

Michael Winter

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.

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

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.org/>

There is just one syntax error that has crippled the entire page, but I'll
suggest several changes.

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.
<head>
<title>Form Test</title>
<script language="JavaScript>

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 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.
function copy(){
var firstField = new Array();
var secondField = new Array();

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];
for(j=0; j<=4; j++){
firstField[j] = document.myForm.numText;
}

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]

Be sure to remove this with the other comment delimiter.
</script>
</head>
<body bgcolor="#FFFFFF">

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
 
M

Mark Hannon

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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,744
Messages
2,569,484
Members
44,903
Latest member
orderPeak8CBDGummies

Latest Threads

Top