Welcome to WebmasterWorld Guest from 54.159.246.164

Forum Moderators: open

Message Too Old, No Replies

Validating a javascript calculator

   
6:28 pm on Jul 4, 2003 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



A few months ago I wrote a toy javascript calculator with much help from this community. I was so happy it worked that, at the time, I did not care that it failed to validate even to transitional. The time has now come to make it valid.

There is a table of four forms, two for input, a calculate button, and an output form. Each form has a NAME, which is used to pass the variable name to the javascript code.

So I have a line:

<TD><FORM NAME="elevation" action="calc.js"><input type="text" size="8"></FORM></TD>

which accepts the elevation gain, and in my javascript file I have a line:

feet = parseFloat(document.elevation.elements[0].value)

which gets it ready for further computations.

It works fine, but NAME is clearly not a valid attribute of FORM. But every javascript calculator I have seen uses that construct, including a simple email address verifier from W3Schools [w3schools.com]!

Moving the NAME attribute into the INPUT field did not work (i.e. the program stopped working).

Any help will obviously be much appreciated :) :)

6:37 pm on Jul 4, 2003 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



have you tried using id instead of name?

SN

6:40 pm on Jul 4, 2003 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Thanks for the quick answer!

Alas, wth id (in either form or input) it does not work.

6:47 pm on Jul 4, 2003 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



probably because of the method of access in the js, getelementbyid or all?

SN

6:59 pm on Jul 4, 2003 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



> probably because of the method of access in the js, getelementbyid or all?

I am a complete novice at javascript (I think it is obvious!) and do not understand the question.

My js file simply starts with a function that uses parseFloat to get numeric values.

Thanks!

7:05 pm on Jul 4, 2003 (gmt 0)

WebmasterWorld Senior Member drdoc is a WebmasterWorld Top Contributor of All Time 10+ Year Member



Just skip the name attribute...
Use document.forms[0]... where the number is the form number. (First form = 0, second form = 1, etc.)

getElementById works in DOM compliant browsers... But, if you want the script to work in older browsers, particularly NN4, you need to use the forms array (forms[0])

7:09 pm on Jul 4, 2003 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



More frustration!

The W3C section 17.3 The FORM element [w3.org] does list name as an attribute of form:

This attribute names the element so that it may be referred to from style sheets or scripts. Note. This attribute has been included for backwards compatibility. Applications should use the id attribute to identify elements.

but their validator, with a transitional DOCTYPE, tells me:

Line 24, column 11: there is no attribute "NAME" (explain...).
<form name="myForm"

7:16 pm on Jul 4, 2003 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



WooHoo!

It works and it validates!

Problem posted at 2:28 PM EDT.
Problem solved at 3:05 PM EDT.

WebmasterWorld rocks!

Many thanks.

<edit>I note that my numerous exuberant exclamation marks have been reduced to one per occurrence.</edit>