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

Need help with Javascript Eval()

P: n/a
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
Share this Question
Share on Google+
8 Replies


P: n/a
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

P: n/a
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

P: n/a
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

P: n/a
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

P: n/a
<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

P: n/a
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

P: n/a
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

P: n/a
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 discussion thread is closed

Replies have been disabled for this discussion.