homepage Welcome to WebmasterWorld Guest from
register, free tools, login, search, pro membership, help, library, announcements, recent posts, open posts,
Become a Pro Member

Home / Forums Index / Code, Content, and Presentation / JavaScript and AJAX
Forum Library, Charter, Moderator: open

JavaScript and AJAX Forum

Validating a javascript calculator

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

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)

have you tried using id instead of name?



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

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)

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



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

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



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

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)

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)


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>

Global Options:
 top home search open messages active posts  

Home / Forums Index / Code, Content, and Presentation / JavaScript and AJAX
rss feed

All trademarks and copyrights held by respective owners. Member comments are owned by the poster.
Home ¦ Free Tools ¦ Terms of Service ¦ Privacy Policy ¦ Report Problem ¦ About ¦ Library ¦ Newsletter
WebmasterWorld is a Developer Shed Community owned by Jim Boykin.
© Webmaster World 1996-2014 all rights reserved