By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
446,207 Members | 1,028 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 446,207 IT Pros & Developers. It's quick & easy.

Storing form elements in variables & arrays

P: n/a
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[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="#FFFFFF">

<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"
<br>

<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>
Jul 23 '05 #1
Share this Question
Share on Google+
2 Replies


P: n/a
On Thu, 02 Sep 2004 05:24:33 GMT, Mark Hannon <ha*******@optonline.net>
wrote:
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.
<html>
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 type="text/javascript">
<!--
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

--
Michael Winter
Replace ".invalid" with ".uk" to reply by e-mail.
Jul 23 '05 #2

P: n/a
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!
Jul 23 '05 #3

This discussion thread is closed

Replies have been disabled for this discussion.