473,378 Members | 1,152 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,378 software developers and data experts.

Need help with Javascript Eval()

The problem lies here eval("document.TeeForm.amt.value(S+M)");

S and M suppose to add up and the total suppose to appear on the AMT field
but it didn't. Any help?

============================ CODE
==============================================

<FORM NAME="TeeForm">
<TABLE BORDER=4>
<TR>
<TD>
<INPUT TYPE="text" NAME="Small" Size="16">
<INPUT TYPE="text" NAME="Medium" Size="16">
<INPUT TYPE="text" NAME="amt" Size="16">
<br>
</TD>
</TR>
<TR>
<TD>
<INPUT TYPE="button" VALUE=" s1 " OnClick="TeeForm.Small.value =
'1';javascript:STotal();">
<INPUT TYPE="button" VALUE=" s2 " OnCLick="TeeForm.Small.value =
'2';javascript:STotal();">
<INPUT TYPE="button" VALUE=" s3 " OnClick="TeeForm.Small.value =
'3';javascript:STotal();">
<br>
<INPUT TYPE="button" VALUE=" s4 " OnClick="TeeForm.Small.value =
'4';javascript:STotal();">
<INPUT TYPE="button" VALUE=" s5 " OnCLick="TeeForm.Small.value =
'5';javascript:STotal();">
<br><br>
<INPUT TYPE="button" VALUE=" m1 " OnClick="TeeForm.Medium.value =
'1';javascript:STotal();">
<INPUT TYPE="button" VALUE=" m2 " OnCLick="TeeForm.Medium.value =
'2';javascript:STotal();">
<INPUT TYPE="button" VALUE=" m3 " OnClick="TeeForm.Medium.value =
'3';javascript:STotal();">
<br>
<INPUT TYPE="button" VALUE=" m4 " OnClick="TeeForm.Medium.value =
'4';javascript:STotal();">
<INPUT TYPE="button" VALUE=" m5 " OnCLick="TeeForm.Medium.value =
'5';javascript:STotal();">
<br><br>
</TD>
</TR>
</TABLE>

<script language="JavaScript">
function STotal()
{
var S = document.TeeForm.Small.value
var M = document.TeeForm.Medium.value
eval("document.TeeForm.amt.value(S+M)");

}
</script>
</FORM>
Jul 20 '06 #1
8 3680
an********@dsdexigns.com said the following on 7/20/2006 6:12 PM:
The problem lies here eval("document.TeeForm.amt.value(S+M)");
Actually, that isn't the problem.
S and M suppose to add up and the total suppose to appear on the AMT field
but it didn't. Any help?
Try using code that adds input fields?

Search the archives for "total input fields"

--
Randy
comp.lang.javascript FAQ - http://jibbering.com/faq & newsgroup weekly
Temporarily at: http://members.aol.com/_ht_a/hikksnotathome/cljfaq/
Javascript Best Practices - http://www.JavascriptToolbox.com/bestpractices/
Jul 20 '06 #2
I'm sorry but how do i do that?
this is my first time using newsgroup.

"Randy Webb" <Hi************@aol.comwrote in message
news:GK******************************@comcast.com. ..
an********@dsdexigns.com said the following on 7/20/2006 6:12 PM:
The problem lies here eval("document.TeeForm.amt.value(S+M)");

Actually, that isn't the problem.
S and M suppose to add up and the total suppose to appear on the AMT
field
but it didn't. Any help?

Try using code that adds input fields?

Search the archives for "total input fields"

--
Randy
comp.lang.javascript FAQ - http://jibbering.com/faq & newsgroup weekly
Temporarily at: http://members.aol.com/_ht_a/hikksnotathome/cljfaq/
Javascript Best Practices -
http://www.JavascriptToolbox.com/bestpractices/
Jul 20 '06 #3
an********@dsdexigns.com wrote:
I'm sorry but how do i do that?
this is my first time using newsgroup.
You should first read the FAQ as it will answer frequently asked
questions and also give guidance on how to post to news groups:

<URL:http://www.jibbering.com/faq>
Please don't top post here - trim material that doesn't need to be
quoted and reply below a quote of what you are replying to.

Finally, to search the archives, use Google groups:

<URL:http://groups.google.com/group/comp.lang.javascript/search?group=comp.lang.javascript&q=total+input+fi elds&qt_g=1&searchnow=Search+this+group>

But anyway, reading the FAQ will tell you what you need to know in
regard to accessing form controls, adding values and even when to use
eval.
"Randy Webb" <Hi************@aol.comwrote in message
news:GK******************************@comcast.com. ..
an********@dsdexigns.com said the following on 7/20/2006 6:12 PM:
The problem lies here eval("document.TeeForm.amt.value(S+M)");
Actually, that isn't the problem.
Maybe not *the* problem, it certainly *an* issue :-)

S and M suppose to add up and the total suppose to appear on the AMT
field
but it didn't. Any help?
Try using code that adds input fields?

Search the archives for "total input fields"
Or just read the FAQ.

[...]
comp.lang.javascript FAQ - http://jibbering.com/faq & newsgroup weekly
Temporarily at: http://members.aol.com/_ht_a/hikksnotathome/cljfaq/
Javascript Best Practices -
http://www.JavascriptToolbox.com/bestpractices/
Good advice.

--
Rob.

Jul 20 '06 #4
thnks!
"RobG" <rg***@iinet.net.auwrote in message
news:11*********************@h48g2000cwc.googlegro ups.com...
an********@dsdexigns.com wrote:
I'm sorry but how do i do that?
this is my first time using newsgroup.

You should first read the FAQ as it will answer frequently asked
questions and also give guidance on how to post to news groups:

<URL:http://www.jibbering.com/faq>
Please don't top post here - trim material that doesn't need to be
quoted and reply below a quote of what you are replying to.

Finally, to search the archives, use Google groups:

<URL:http://groups.google.com/group/comp....h?group=comp.l
ang.javascript&q=total+input+fields&qt_g=1&searchn ow=Search+this+group>
>
But anyway, reading the FAQ will tell you what you need to know in
regard to accessing form controls, adding values and even when to use
eval.
"Randy Webb" <Hi************@aol.comwrote in message
news:GK******************************@comcast.com. ..
an********@dsdexigns.com said the following on 7/20/2006 6:12 PM:
The problem lies here eval("document.TeeForm.amt.value(S+M)");
>
Actually, that isn't the problem.

Maybe not *the* problem, it certainly *an* issue :-)

S and M suppose to add up and the total suppose to appear on the AMT
field
but it didn't. Any help?
>
Try using code that adds input fields?
>
Search the archives for "total input fields"

Or just read the FAQ.

[...]
comp.lang.javascript FAQ - http://jibbering.com/faq & newsgroup weekly
Temporarily at: http://members.aol.com/_ht_a/hikksnotathome/cljfaq/
Javascript Best Practices -
http://www.JavascriptToolbox.com/bestpractices/

Good advice.

--
Rob.

Jul 20 '06 #5
<an********@dsdexigns.comwrites:
The problem lies here eval("document.TeeForm.amt.value(S+M)");
Much of the problem lies here.
S and M suppose to add up and the total suppose to appear on the AMT field
but it didn't.
Then what happened? Did you get an error? (Are error reports turned on
in your browser?) Was something wrong put in the field?

Any help?
I'll start from the top :)
<FORM NAME="TeeForm">
I recommend adding an "id" attribute too, which is really the correct
thing to do, and the "name" attribute is only for backwards
compatability:
<form id="TeeForm" name="TeeForm">
<INPUT TYPE="button" VALUE=" s1 " OnClick="TeeForm.Small.value =
'1';javascript:STotal();">
The "javascript:" is superflous in the onclick attribute. I recommend
changing this onclick attribute to something like:
onclick="this.form.elements['Small'].value='1'; sumTotal(this.form);"

I added a reference to the form as an argument to sumTotal function
so that the function has easier access to the form. I'll change the
function below to match this.
(I renamed STotal since tradition has javascript function names
starting with lower case letters)

Generally, you don't want to write "javascript:". It's not necessary
in event handlers (like onclick), and it's a very bad idea in links.
If you see "javascript:", then assume something needs to be fixed.
<script language="JavaScript">
Should be:
<script type="text/javascript">
The "type" attribute is required for valid HTML, and is always sufficient.
function STotal()
{
var S = document.TeeForm.Small.value
I recommend accessing forms and form controls using the document.forms
and form.elements collections. It's not always necessary, but it *always*
works.
var s = document.forms['TeeForm'].elements['Small'].value;

However, if you have the form as a parameter of the function, you
can start from there instead of looking up the form every time
you need it.
var M = document.TeeForm.Medium.value
eval("document.TeeForm.amt.value(S+M)");
There is absolutely no need for "eval" here. Generally, there is no
need for eval, and any occurence of it should be considered a warning
sign that the author is not entirely sure what he's doing.

In this case,
eval("document.TeeForm.amt.value(S+M)");
is completely equivalent to:
document.TeeForm.amt.value(S+M);
which doesn't work for two reasons.
First, S+M is performing string concatenation, since S and M are string
values (all input control values are). You should convert these to numbers
before adding them, e.g.:
Number(S)+Number(M)
Second, it is treating the value of the "amt" control as a function. As
just stated, it is a string, so that won't work. You'll want to assign
to the value property instead.

Here's a suggestion for a revised sum function:

function sumTotal(form) {
var s = form.elements['Small'].value;
var m = form.elements['Medium'].value;
var sum = Number(s) + Number(m);
form.elements['amt'].value = sum;
}

Good luck
/L
--
Lasse Reichstein Nielsen - lr*@hotpop.com
DHTML Death Colors: <URL:http://www.infimum.dk/HTML/rasterTriangleDOM.html>
'Faith without judgement merely degrades the spirit divine.'
Jul 21 '06 #6
JRS: In article <e9**********@mawar.singnet.com.sg>, dated Fri, 21 Jul
2006 06:12:12 remote, seen in news:comp.lang.javascript,
an********@dsdexigns.com posted :
>
<script language="JavaScript">
^^^^^^^^^^^^^^^^^^^^^ <FAQENTRYDeprecated
function STotal()
{
var S = document.TeeForm.Small.value
var M = document.TeeForm.Medium.value
eval("document.TeeForm.amt.value(S+M)");

}
</script>

Randy should have advised you to read the newsgroup FAQ. Do so before
replying. Read all of it.

Change three lines to

var S = +document.TeeForm.Small.value
var M = +document.TeeForm.Medium.value
document.TeeForm.amt.value = S+M

and the script will then operate in at least one browser.

It's not clear what all those buttons are for; you should gave
simplified the test to a single button and explained in words what you
want to happen.

--
© John Stockton, Surrey, UK. ?@merlyn.demon.co.uk Turnpike v4.00 IE 4 ©
<URL:http://www.jibbering.com/faq/>? JL/RC: FAQ of news:comp.lang.javascript
<URL:http://www.merlyn.demon.co.uk/js-index.htmjscr maths, dates, sources.
<URL:http://www.merlyn.demon.co.uk/TP/BP/Delphi/jscr/&c, FAQ items, links.
Jul 21 '06 #7
JRS: In article <e9**********@mawar.singnet.com.sg>, dated Fri, 21 Jul
2006 07:24:52 remote, seen in news:comp.lang.javascript,
an********@dsdexigns.com posted :
>thnks!
"RobG" <rg***@iinet.net.auwrote in message
news:11*********************@h48g2000cwc.googlegr oups.com...
>an********@dsdexigns.com wrote:
I'm sorry but how do i do that?
this is my first time using newsgroup.

You should first read the FAQ as it will answer frequently asked
questions and also give guidance on how to post to news groups:

<URL:http://www.jibbering.com/faq>

Evidently pig-headed and best plonked.

--
© John Stockton, Surrey, UK. yyww merlyn demon co uk Turnpike v4.00 MIME ©
Web <URL:http://www.uwasa.fi/~ts/http/tsfaq.html-Timo Salmi: Usenet Q&A.
Web <URL:http://www.merlyn.demon.co.uk/news-use.htm: about usage of News.
No Encoding. Quotes before replies. Snip well. Write clearly. Don't Mail News.
Jul 21 '06 #8
Dr John Stockton said the following on 7/21/2006 6:46 AM:
JRS: In article <e9**********@mawar.singnet.com.sg>, dated Fri, 21 Jul
2006 06:12:12 remote, seen in news:comp.lang.javascript,
an********@dsdexigns.com posted :
> <script language="JavaScript">
^^^^^^^^^^^^^^^^^^^^^ <FAQENTRYDeprecated
> function STotal()
{
var S = document.TeeForm.Small.value
var M = document.TeeForm.Medium.value
eval("document.TeeForm.amt.value(S+M)");

}
</script>


Randy should have advised you to read the newsgroup FAQ.
That is a border-line TL'ish thing to even think. "Could have", yes.
"Should have"? Not even close my friend.

--
Randy
comp.lang.javascript FAQ - http://jibbering.com/faq & newsgroup weekly
Javascript Best Practices - http://www.JavascriptToolbox.com/bestpractices/
Jul 21 '06 #9

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

4
by: fartsniff | last post by:
Hello all. Can someone help out ? I found this PHP code that works just fine, however, I am trying to put it back into its original Javascript form. I am still learning the syntax, etc. for...
4
by: Sarah | last post by:
Hi all. I have a form, and several text and image links on it that should submit the form with different actions. I prepared a simple page with just the code that's not working. PROBLEM:...
4
by: JesusFreak | last post by:
From: us_traveller@yahoo.com (JesusFreak) Newsgroups: microsoft.public.scripting.jscript Subject: toolbar script problem NNTP-Posting-Host: 192.92.126.136 Recently, I downloaded the following...
3
by: P Wolpert | last post by:
This is my first post. I hope I don't sound stupid. I have a script conflict when I put two scripts on one page. Both scripts will work if I use one at a time but the menu button script will not...
4
by: binnyva | last post by:
Hello Everybody, I am writing an interactive tutorial for JavaScript. I created a text box into which the users can input javascript commands. On pressing a button, these commands will be...
14
by: enrique | last post by:
I'm trying to debug my expression that matches an alphanumeric with any number of dashes (including none), except at the ends and obviously disallowing two or more consecutive dashes. Here it...
2
by: u1jd | last post by:
Hi, I am trying to do the following but i get an error, is there no way i can use eval to print have it assigned to a predeterminded variable. eval (object_name + "_CalCalendar") = new...
2
by: this one | last post by:
I have the following code <html> <head> <title>Untitled Document</title> <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1"> <script language="JavaScript"...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.