Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
Home
Discussion GroupsGeneralPHPASPPerlColdFusionFlashHTML, CSS, ScriptsBrowsers

Webmaster Forum / HTML, CSS, Scripts / JavaScript / February 2006



Tip: Looking for answers? Try searching our database.

Calculate field values using javascript

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
mehere - 26 Feb 2006 02:03 GMT
Please excuse me if I am posting in wrong NG.

Can someone explain how I can use javascript (I am assuming this is the best
way to do it) to add two field values together.  I.e. field 1 plus field 2
equals field 3 (this is in a php form)

Also how can I set a field to be a value depending on what the user selects,
i.e. they may have eight items to select from and each one has a different
value.

The above javascript will then use that value to add to another field value
to give a me a total value

I know this should be simple and I am looking around and reading how to do
this, but any pointers or sharable code would be helpful.

Cheers

Greg
Hal Rosser - 26 Feb 2006 04:27 GMT
> Please excuse me if I am posting in wrong NG.
>
[quoted text clipped - 11 lines]
> I know this should be simple and I am looking around and reading how to do
> this, but any pointers or sharable code would be helpful.

There are many examples at this website:
http://www.w3schools.com/js/js_examples.asp
some folks say the site has many errors - maybe so - but it also has many
good examples.
Take a look and decide for yourself.
Dr John Stockton - 27 Feb 2006 11:57 GMT
JRS:  In article <74aMf.44599$bW.19883@bignews8.bellsouth.net>, dated
Sat, 25 Feb 2006 23:27:39 remote, seen in news:comp.lang.javascript, Hal
Rosser <hmrosser@bellsouth.net> posted :

>> Can someone explain how I can use javascript (I am assuming this is the
>best
[quoted text clipped - 9 lines]
>value
>> to give a me a total value

>There are many examples at this website:
>http://www.w3schools.com/js/js_examples.asp
>some folks say the site has many errors - maybe so - but it also has many
>good examples.
>Take a look and decide for yourself.

If the OP knows as little as he asserts, he will not be able (with
reasonable effort) to judge which parts, if any, are good.

Actually, there are two ways of adding; if one adds "cat" and "fish" one
gets "catfish", but if one adds 1 and 2 one gets 3.  Javascript supports
both of those, alas with the same operator.

Generally, one wants to do more than the OP asks; hence a slightly more
general solution will be more informative.

Let ADDR stand for some way of addressing a field.

Then   ADDR.value   is, for the sort of control in question, a String
holding the content of the field.  If that string represents a number,
then   +ADDR.value   gives a Number of appropriate value.

So:     var Qty1 = +ADDR1.value
       var Qty2 = +ADDR2.value
       ADDR3.value = Qty1 + Qty2 // auto-converted to String

For the second case, use selectedIndex to address the field chosen, like
 PlaceValue.value  = + Places.options[Places.selectedIndex].value

Read the newsgroup FAQ.

Signature

© 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.htm> jscr maths, dates, sources.
<URL:http://www.merlyn.demon.co.uk/> TP/BP/Delphi/jscr/&c, FAQ items, links.

Jonas Raoni - 28 Feb 2006 08:10 GMT
> So:     var Qty1 = +ADDR1.value
>         var Qty2 = +ADDR2.value
>         ADDR3.value = Qty1 + Qty2 // auto-converted to String

I preffer:

+field.value || 0

Then, NaN values will be converted to 0

Signature

Jonas Raoni Soares Silva
http://www.jsfromhell.com

Dr John Stockton - 28 Feb 2006 23:13 GMT
JRS:  In article <du10ih$tc6$4@emma.aioe.org>, dated Tue, 28 Feb 2006
05:10:57 remote, seen in news:comp.lang.javascript, Jonas Raoni
<jonasraoni@gmail.com> posted :
>> So:     var Qty1 = +ADDR1.value
>>         var Qty2 = +ADDR2.value
[quoted text clipped - 5 lines]
>
>Then, NaN values will be converted to 0

In general, NaN values should not be converted to zero; they should
result in a form of error message suited to the application.

Signature

© 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.htm> jscr maths, dates, sources.
<URL:http://www.merlyn.demon.co.uk/> TP/BP/Delphi/jscr/&c, FAQ items, links.

mehere - 28 Feb 2006 11:32 GMT
> JRS:  In article <74aMf.44599$bW.19883@bignews8.bellsouth.net>, dated
> Sat, 25 Feb 2006 23:27:39 remote, seen in news:comp.lang.javascript, Hal
[quoted text clipped - 46 lines]
>
> Read the newsgroup FAQ.

OP here

Thanks and yes VERY new to this cheers for your help

Greg
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2009 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.