why function not working

A

ANDREW POLLOCK

I'm trying to have the values in two text box added and displayed in the 3rd
box. Where did I go wrrong?
<HTML>
<HEAD>
<TITLE>Untitled Document</TITLE>
<META HTTP-EQUIV="Content-Type" CONTENT="text/html; charset=iso-8859-1">
<Script Language="JavaScript">
function "zAddIt()"
{document.sandy.zresult.value=document.sandy.first.value+document.sandy.second.value}</Script></HEAD><BODY BGCOLOR="#FFFFFF"><FORM Name="sandy" METHOD="post" ACTION=""> <P> <INPUT TYPE="text" NAME="first" onChange="zAddIt()"> </P> <P> <INPUT TYPE="text" NAME="second" onChange="zAddIt()"> </P> <P> <INPUT TYPE="text" NAME="zresult"> </P></FORM></BODY></HTML>
 
C

Captain Paralytic

I'm trying to have the values in two text box added and displayed in the 3rd
box. Where did I go wrrong?
This is easy to answer.
You didn't bother to lay your code out legibly so that you could see
if anything was missing.
Then you posted the complete mess here, so you'd be extremely lucky to
find anyone who could be bothered to spend their time laying it out
neatly for you!
 
A

ANDREW POLLOCK

Hope this is easier to read:
I'm trying to have the values in two text box added and displayed in the 3rd
box. Where did I go wrrong?
<HTML>
<HEAD>
<TITLE>Untitled Document</TITLE>
<META HTTP-EQUIV="Content-Type"
CONTENT="text/html; charset=iso-8859-1">
<Script Language="JavaScript">
function "zAddIt()"
{document.sandy.zresult.value=document.sandy.first.value+
document.sandy.second.value}
</Script>
</HEAD>
<BODY BGCOLOR="#FFFFFF">
<FORM Name="sandy" METHOD="post" ACTION=""> <P>
<INPUT TYPE="text" NAME="first" onChange="zAddIt()"> </P> <P>
<INPUT TYPE="text" NAME="second" onChange="zAddIt()"> </P> <P>
<INPUT TYPE="text" NAME="zresult"> </P></FORM></BODY></HTML>
 
C

Captain Paralytic

Hope this is easier to read:
I'm trying to have the values in two text box added and displayed in the 3rd
box. Where did I go wrrong?
<HTML>
<HEAD>
<TITLE>Untitled Document</TITLE>
<META HTTP-EQUIV="Content-Type"
   CONTENT="text/html; charset=iso-8859-1">
  <Script Language="JavaScript">
   function "zAddIt()"
    {document.sandy.zresult.value=document.sandy.first.value+
     document.sandy.second.value}
</Script>
</HEAD>
<BODY BGCOLOR="#FFFFFF">
<FORM Name="sandy" METHOD="post" ACTION="">  <P>
<INPUT TYPE="text" NAME="first" onChange="zAddIt()">  </P>  <P>
<INPUT TYPE="text" NAME="second" onChange="zAddIt()">  </P>  <P>
<INPUT TYPE="text" NAME="zresult">  </P></FORM></BODY></HTML>

Better I guess, but if you want to be able to write anything decent I
strongly suggest you take the time and care to lay out your code
(whether javascript or HTML) neatly with proper indentation. If you
can't be bothered to do that, then I would give up right now.

Try removing the quotes round the function name.
 
E

Erwin Moller

Captain Paralytic schreef:
Better I guess, but if you want to be able to write anything decent I
strongly suggest you take the time and care to lay out your code
(whether javascript or HTML) neatly with proper indentation. If you
can't be bothered to do that, then I would give up right now.

Try removing the quotes round the function name.

Good advice, both the indention and the removal of the quotes around the
functionname.
A few additions:
1) Try adding a doctype (and validate at w3c if you care)
2) <Script Language="JavaScript"> is obsolete in these days.
Better would be:
<script type="text/javascript">
3) Why do you have a bgcolor of #FFFFFF? I think that is pretty much
default.


Regards,
Erwin Moller


--
"There are two ways of constructing a software design: One way is to
make it so simple that there are obviously no deficiencies, and the
other way is to make it so complicated that there are no obvious
deficiencies. The first method is far more difficult."
-- C.A.R. Hoare
 
A

ANDREW POLLOCK

I made the changes you suggested and tweaked it to pass the validator, but
it still doesn't work. After validating:<!DOCTYPE html PUBLIC "-//W3C//DTD
HTML 4.01 Transitional//EN"
"http://www.w3.org/TR/html4/loose.dtd">

<HTML>
<HEAD>
<TITLE>Untitled Document</TITLE>
<META HTTP-EQUIV="Content-Type" CONTENT="text/html;
charset=iso-8859-1">
<script type="text/javascript">
function zAddIt()
{document.sandy.zresult.value=document.sandy.first.value+document.sandy.second.value
}
</Script>

</HEAD>
<BODY BGCOLOR="#FFFFFF">
<FORM NAME="sandy" METHOD="post" ACTION="">


<P>
<INPUT TYPE="text" NAME="first" onChange="zAddIt()">
</P>
<P>
<INPUT TYPE="text" NAME="second" onChange="zAddIt()">
</P>
<P>
<INPUT TYPE="text" NAME="zresult">
</P>

</FORM>
</BODY>
</HTML>
 
A

ANDREW POLLOCK

You are right. Thanks all! I had NoScript on and forgot it would treat a
script on a local computer just like an internet one. I do want to change
it so as to add the values instead of concatenate.
 
C

Captain Paralytic

You are right. Thanks all! I had NoScript on and forgot it would treat a
script on a local computer just like an internet one.  I do want to change
it so as to add the values  instead of concatenate.

Feel free to do so, and please do not top post (top posting fixed)
 
H

Herbert Blenner

I'm trying to have the values in two text box added and displayed in the 3rd
box. Where did I go wrrong?
<HTML>
<HEAD>
<TITLE>Untitled Document</TITLE>
<META HTTP-EQUIV="Content-Type" CONTENT="text/html; charset=iso-8859-1">
<Script Language="JavaScript">
function "zAddIt()"
 {document.sandy.zresult.value=document.sandy.first.value+document.sandy.sec­ond.value}</Script></HEAD><BODY BGCOLOR="#FFFFFF"><FORM Name="sandy" METHOD="post" ACTION="">  <P>    <INPUT TYPE="text" NAME="first" onChange="zAddIt()">  </P>  <P>    <INPUT TYPE="text" NAME="second" onChange="zAddIt()">  </P>  <P>    <INPUT TYPE="text" NAME="zresult">  </P></FORM></BODY></HTML>

I suugest that you change zAddit as follows.

function zAddIt()
{
if (document.sandy.first.value!="" && document.sandy.second.value!="")
{
document.sandy.zresult.value=parseInt(document.sandy.first.value)+
parseInt(document.sandy.second.value)
}
}

Good luck.

Herbert
 
D

Dr J R Stockton

In comp.lang.javascript message <[email protected]
Hope this is easier to read:
I'm trying to have the values in two text box added and displayed in the 3rd
box. Where did I go wrrong?

Your REAL problems are that you did not test your code in a browser with
Error Console, or equivalent, turned on (may need Developer on); and
that you had not read the newsgroup FAQ effectively. Also, you did not
say what actually happened.

With the first done, IE, Firefox, Opera, Safari. Chrome all indicate the
line number of the (first) error, some are more explicit.



<FAQENTRY> 11.1 How do I get my browser to report JavaScript errors?

I think it would be useful to split that into two; the first for what is
available in a freshly-installed browser, the second for add-ons.

It could be useful to split into internal subsections by OS for Windows,
Mac, UNIX, Others (which would show more clearly where more is needed).

It's a good idea to read the newsgroup c.l.j and its FAQ. See below.
 
D

Dr J R Stockton

In comp.lang.javascript message <56bf8342-2066-4151-a44f-29a5c36f8f01@l1
3g2000yqb.googlegroups.com>, Wed, 9 Sep 2009 00:01:41, Herbert Blenner
I suugest that you change zAddit as follows.

function zAddIt()
{
if (document.sandy.first.value!="" && document.sandy.second.value!="")
{
document.sandy.zresult.value=parseInt(document.sandy.first.value)+
parseInt(document.sandy.second.value)
}
}

Only rather rarely should parseInt be used with a single argument; read
the FAQ to see why.

It would be much better to test the .values with a RegExp such as
(unchecked) /^\d+$/ or /^\d+(\.\d+)?$/ and then to convert with a unary
minus.


function zAddIt() {
var V1 = document.sandy.first.value, V2 = document.sandy.second.value
function Chk(V) {
if (!/^\d+(\.\d+)?$/.test(V)) { alert(V + "is bad") ; return NaN }
return +V }
document.sandy.zresult.value = Chk(V1) + Chk(V2) } // untested


Code is generally more readable if controls are read only once each,
with the raw value stored in a suitable variable.
 
H

Herbert Blenner

In comp.lang.javascript message <56bf8342-2066-4151-a44f-29a5c36f8f01@l1
3g2000yqb.googlegroups.com>, Wed, 9 Sep 2009 00:01:41, Herbert Blenner
<[email protected]> posted:








Only rather rarely should parseInt be used with a single argument; read
the FAQ to see why.

It would be much better to test the .values with a RegExp such as
(unchecked) /^\d+$/ or /^\d+(\.\d+)?$/ and then to convert with a unary
minus.

function zAddIt() {
  var V1 = document.sandy.first.value, V2 = document.sandy.second.value
  function Chk(V) {
    if (!/^\d+(\.\d+)?$/.test(V)) { alert(V + "is bad") ; return NaN }
    return +V }
  document.sandy.zresult.value = Chk(V1) + Chk(V2) } // untested

Code is generally more readable if controls are read only once each,
with the raw value stored in a suitable variable.

--
 (c) John Stockton, Surrey, UK.  [email protected]   Turnpikev6.05   MIME.
 Web  <URL:http://www.merlyn.demon.co.uk/> - FAQish topics, acronyms,& links.
 Proper <= 4-line sig. separator as above, a line exactly "-- " (SonOfRFC1036)
 Do not Mail News to me. Before a reply, quote with ">" or "> " (SonOfRFC1036)- Hide quoted text -

- Show quoted text -

Thanks for the advice.

After posting, I had second thoughts about using parseInt, since the
OP did not exclude a user from entering a mixed or a floating point
number.

So how would you handle these possibilities?

Hebert
 
E

Evertjan.

Dr J R Stockton wrote on 09 sep 2009 in comp.lang.javascript:
function zAddIt() {
var V1 = document.sandy.first.value, V2 = document.sandy.second.value
function Chk(V) {
if (!/^\d+(\.\d+)?$/.test(V)) { alert(V + "is bad") ; return NaN }
return +V }
document.sandy.zresult.value = Chk(V1) + Chk(V2) } // untested


Code is generally more readable if controls are read only once each,
with the raw value stored in a suitable variable.

btw:

document.sandy.first.value

is not very reliable crossbrowserwize, use something like:

var V1 = document.getElementById('sandy').first.value

or even safer, declare:

var sandy = document.getElementById('sandy')
var V1 = sandy.first.value
var V2 = sandy.second.value
 
D

Dr J R Stockton

In comp.lang.javascript message <36a56f43-a110-4759-a7dc-04931946d8e7@y3
6g2000yqh.googlegroups.com>, Wed, 9 Sep 2009 16:49:35, Herbert Blenner
After posting, I had second thoughts about using parseInt, since the
OP did not exclude a user from entering a mixed or a floating point
number.

So how would you handle these possibilities?


Firstly, you should read about news-posting, in the FAQ and elsewhere.

Secondly, you should read the code that I posted and you quoted.

Thirdly, you could read
<URL:http://www.merlyn.demon.co.uk/js-valid.htm>.


Concerning what Evertjan wrote : in the common case where everything is
in one <FORM>, one can give the routine an argument F, supplied in the
call as this.form , and refer to the controls as, for example, F.first
(.value). There is then no need for the form to be named. It seems
neater to me.
 

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

Forum statistics

Threads
474,089
Messages
2,570,602
Members
47,222
Latest member
jspanther

Latest Threads

Top