473,385 Members | 2,014 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,385 software developers and data experts.

Want to auto-add form inputs to a grand total.

Can someone help me modify this for future Usenet archival and to help
me solve what I don't know?

<form name="addform" method="POST" action="./submit.php">
<input type="text" name="Box 1" size="20" value="100.50"> +
<input type="text" name="Box 2" size="20" value="200.50"> +
<input type="text" name="Box 3" size="20" value="1.50"> =
<input type="text" name="Total" size="20" value="302.50"> </p>
</form>

I want someone to be able to dynamically type any number in Box 1, 2
and 3 and it autosum the total on the Total box -- all without pressing
a "calculate" button. Thank you so much for anyone's genius and help!

Oct 13 '05 #1
10 5945
gi******************@gmail.com said the following on 10/12/2005 8:45 PM:
Can someone help me modify this for future Usenet archival and to help
me solve what I don't know?
Why? It has been "solved" so many times in the past and is already
archived for those willing to search for the answer.
<form name="addform" method="POST" action="./submit.php">
<input type="text" name="Box 1" size="20" value="100.50"> +
<input type="text" name="Box 2" size="20" value="200.50"> +
<input type="text" name="Box 3" size="20" value="1.50"> =
<input type="text" name="Total" size="20" value="302.50"> </p>
</form>

I want someone to be able to dynamically type any number in Box 1, 2
and 3 and it autosum the total on the Total box -- all without pressing
a "calculate" button. Thank you so much for anyone's genius and help!


Search the archives, the answers are already there.

--
Randy
comp.lang.javascript FAQ - http://jibbering.com/faq & newsgroup weekly
Oct 13 '05 #2
I ran a second Google search. What I mean is a working example. I still
find discussions about the topic, but nothing helping me as a complete
novice. I'm hoping someone can help me. If I missed something out there
then please forgive this response. Thank you.

Carly

Oct 13 '05 #3
gi******************@gmail.com said the following on 10/12/2005 11:21 PM:
I ran a second Google search.
A Google search of the web or an archive search of comp.lang.javascript?
What I mean is a working example. I still find discussions about the
topic, but nothing helping me as a complete novice. I'm hoping someone
can help me. If I missed something out there then please forgive this
response.
<form name="addform" method="POST" action="./submit.php">
<input type="text" name="Box1" size="20" value="100.50"
onchange="totalIt(this.form)"> +
<input type="text" name="Box2" size="20" value="200.50"
onchange="totalIt(this.form)"> +
<input type="text" name="Box3" size="20" value="1.50"
onchange="totalIt(this.form)">
<input type="text" name="Total" size="20" value="302.50"> </p>
</form>
<script type="text/javascript">
function totalIt(formObj){
formObj.Total.value=(+formObj.Box1.value)+(+formOb j.Box2.value)+(+formObj.Box3.value);
}
</script>

Note: I renamed your inputs without spaces in the name so that I could
use the dot notation.

<URL:
http://groups.google.com/group/micro...6bd95dc8513417


will give you some good reading also.

Please quote what you are replying to.

If you want to post a followup via groups.google.com, don't use the
"Reply" link at the bottom of the article. Click on "show options" at
the top of the article, then click on the "Reply" at the bottom of the
article headers.

--
Randy
comp.lang.javascript FAQ - http://jibbering.com/faq & newsgroup weekly
Answer:It destroys the order of the conversation
Question: Why?
Answer: Top-Posting.
Question: Whats the most annoying thing on Usenet?
Oct 13 '05 #4
Thank you, nicely done. Appreciate it!

Randy Webb wrote:
gi******************@gmail.com said the following on 10/12/2005 11:21 PM:
I ran a second Google search.


A Google search of the web or an archive search of comp.lang.javascript?
What I mean is a working example. I still find discussions about the
topic, but nothing helping me as a complete novice. I'm hoping someone
can help me. If I missed something out there then please forgive this
response.


<form name="addform" method="POST" action="./submit.php">
<input type="text" name="Box1" size="20" value="100.50"
onchange="totalIt(this.form)"> +
<input type="text" name="Box2" size="20" value="200.50"
onchange="totalIt(this.form)"> +
<input type="text" name="Box3" size="20" value="1.50"
onchange="totalIt(this.form)">
<input type="text" name="Total" size="20" value="302.50"> </p>
</form>
<script type="text/javascript">
function totalIt(formObj){
formObj.Total.value=(+formObj.Box1.value)+(+formOb j.Box2.value)+(+formObj.Box3.value);
}
</script>

Note: I renamed your inputs without spaces in the name so that I could
use the dot notation.

<URL:
http://groups.google.com/group/micro...6bd95dc8513417
>


will give you some good reading also.

Please quote what you are replying to.

If you want to post a followup via groups.google.com, don't use the
"Reply" link at the bottom of the article. Click on "show options" at
the top of the article, then click on the "Reply" at the bottom of the
article headers.

--
Randy
comp.lang.javascript FAQ - http://jibbering.com/faq & newsgroup weekly
Answer:It destroys the order of the conversation
Question: Why?
Answer: Top-Posting.
Question: Whats the most annoying thing on Usenet?


Oct 13 '05 #5
One more thing - how do I pad the final result (assuming this is a cash
balance sheet), so 2.5 will appear as 2.50?

Randy Webb wrote:
gi******************@gmail.com said the following on 10/12/2005 11:21 PM:
I ran a second Google search.


A Google search of the web or an archive search of comp.lang.javascript?
What I mean is a working example. I still find discussions about the
topic, but nothing helping me as a complete novice. I'm hoping someone
can help me. If I missed something out there then please forgive this
response.


<form name="addform" method="POST" action="./submit.php">
<input type="text" name="Box1" size="20" value="100.50"
onchange="totalIt(this.form)"> +
<input type="text" name="Box2" size="20" value="200.50"
onchange="totalIt(this.form)"> +
<input type="text" name="Box3" size="20" value="1.50"
onchange="totalIt(this.form)">
<input type="text" name="Total" size="20" value="302.50"> </p>
</form>
<script type="text/javascript">
function totalIt(formObj){
formObj.Total.value=(+formObj.Box1.value)+(+formOb j.Box2.value)+(+formObj.Box3.value);
}
</script>

Note: I renamed your inputs without spaces in the name so that I could
use the dot notation.

<URL:
http://groups.google.com/group/micro...6bd95dc8513417
>


will give you some good reading also.

Please quote what you are replying to.

If you want to post a followup via groups.google.com, don't use the
"Reply" link at the bottom of the article. Click on "show options" at
the top of the article, then click on the "Reply" at the bottom of the
article headers.

--
Randy
comp.lang.javascript FAQ - http://jibbering.com/faq & newsgroup weekly
Answer:It destroys the order of the conversation
Question: Why?
Answer: Top-Posting.
Question: Whats the most annoying thing on Usenet?


Oct 13 '05 #6
gi******************@gmail.com wrote:
One more thing - how do I pad the final result (assuming this is a cash
balance sheet), so 2.5 will appear as 2.50?


v="2.5"
v+=v.indexOf(".")==-1?".00":v.indexOf(".")==v.length-2?"0":"";

Mick

[snip]
Oct 13 '05 #7
gi******************@gmail.com wrote:
One more thing - how do I pad the final result (assuming this is a cash
balance sheet), so 2.5 will appear as 2.50?

[...]

Here's a commented example, modified to suit your circumstance.

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN"
"http://www.w3.org/TR/html4/strict.dtd">
<html>
<head>
<title>Add form values</title>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">

<style type="text/css">
#addform input {width: 5em; text-align: right;}
..eCell {width:5em; color: red; font-weight:bold;}
</style>

<script type="text/javascript">

function updateForm(el)
{
// Declare variables
var f = el.form; // A reference to the form
var total = 0; // Initialise total as a number
var i = 1; // The first number of our 'Value' sequence
var err = false; // Has an error been found?
var erEl, m, x; // See below

// For the contiguous sequence of form controls with a name
// of 'Value1', 'Value2', ...
while ( (x=f.elements['Value' + i++]) ){

// Get the element that errors will be written to
erEl = document.getElementById('er_' + x.id);

// If validMoney doesn't return false it will return a
// formatted string
if ( (m = validMoney(x.value))){

// Clear the error element (regardless of whether it needs it)
erWrite('', erEl);

// Write the formatted value back to the control
x.value = m;

// Add the value to the total - the unary '+' converts m
// from a string to a number
total += +m;

// But if validMoney returned false, the value isn't one we like
} else {

// Warn the the user to fix the value
erWrite('Check!', erEl);

// Note that we found an error
anError = true;
}
}

// Write a value to the control named 'Total' -
// if an error was found, write nothing, else write the total
f.elements['Total'].value = (err)? '' : validMoney(total);
}

// Given an input element, write txt into it
// If no element is passed, use an alert
function erWrite(txt, el)
{
if (el) {
el.innerHTML = txt;
} else {
alert(txt);
}
}

// Check that n is a valid number. If so, return
// the formatted number. Otherwise, return false.
function validMoney(n)
{
// Convert to cents and round
var m = Math.round(n*100)+'';

// Add a leading zero if the number is less than 10
// If above made m 'NaN', test is false, no added zero
if (m<10) m = '0' + m;

// Return false if m is not a number (isNaN)
// or a formatted string if it is.
return (isNaN(m))? false :
(m/100 | 0) + '.' + m.substring(m.length-2);

// m/100 | 0 returns the integer part of m/100 or
// zero if the result is less than 1 - c.f. Math.floor(m/100)
// m.substring(...) returns the last two characters of m.
}

</script>

</head>
<body>

<!--
The form controls that will have their values added are named
Value1, Value2, etc. in a contiguous sequence.

Each has an associated element that error messages are
written to. The error element has an ID of the associated
control ID prefixed by 'er_'.

e.g. for Value1 the associated error element is er_Value1.

Errors are written using innerHTML.

The total is written to a control call 'Total'
-->
<form name="addform" id="addform" action="">
<table>
<tr>
<td>Value 1:</td>
<td><input type="text" name="Value1" id="Value1" value=".04"
onblur="updateForm(this);"></td>
<td class="eCell" id="er_Value1"></td>
</tr>
<tr>
<td>Value 2:</td>
<td><input type="text" name="Value2" id="Value2" value="200"
onblur="updateForm(this);"></td>
<td class="eCell" id="er_Value2"></td>
</tr>
<tr>
<td>Value 3:</td>
<td><input type="text" name="Value3" id="Value3" value="1."
onblur="updateForm(this);"></td>
<td class="eCell" id="er_Value3"></td>
</tr>
<tr>
<td>Total:</td>
<td><input type="text" name="Total" id="Total" readonly></td>
<td><input type="reset"></td>
</tr>
</table>
</form>

</body>
</html>
--
Rob
Oct 13 '05 #8
JRS: In article <44*****************@news.optus.net.au>, dated Thu, 13
Oct 2005 23:28:00, seen in news:comp.lang.javascript, RobG
<rg***@iinet.net.au> posted :
// Convert to cents and round
var m = Math.round(n*100)+'';

// Add a leading zero if the number is less than 10
// If above made m 'NaN', test is false, no added zero
if (m<10) m = '0' + m;


Since m is a String, it might be considered more elegant to test its
length rather than convert back to Number for comparison. Though it
then needs modification to deal with negative input acceptably.

Hoe about starting
var E, S, J
S = String(J = Math.round(n*100))
and using J/100 ?
Perhaps there should be a note that n must be not below -0.005?
And not above 2^31-1?

--
© John Stockton, Surrey, UK. ?@merlyn.demon.co.uk Turnpike v4.00 MIME. ©
Web <URL:http://www.merlyn.demon.co.uk/> - FAQqish topics, acronyms & links;
Astro stuff via astron-1.htm, gravity0.htm ; quotings.htm, pascal.htm, etc.
No Encoding. Quotes before replies. Snip well. Write clearly. Don't Mail News.
Oct 15 '05 #9
JRS: In article <11*********************@g43g2000cwa.googlegroups. com>,
dated Thu, 13 Oct 2005 09:36:00, seen in news:comp.lang.javascript,
gi******************@gmail.com posted :
One more thing - how do I pad the final result (assuming this is a cash
balance sheet), so 2.5 will appear as 2.50?


See FAQ Sec 4.6. It's all due to the Euro; it was not needed with Lire.

--
© 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.
Oct 15 '05 #10
Dr John Stockton said the following on 10/15/2005 12:08 PM:
JRS: In article <11*********************@g43g2000cwa.googlegroups. com>,
dated Thu, 13 Oct 2005 09:36:00, seen in news:comp.lang.javascript,
gi******************@gmail.com posted :

One more thing - how do I pad the final result (assuming this is a cash
balance sheet), so 2.5 will appear as 2.50?

See FAQ Sec 4.6. It's all due to the Euro; it was not needed with Lire.


No John, wanting 2.5 displayed as 2.50 is not "due to the Euro". It is
due to - gasp - people wanting the leading/trailing zero displayed much
as it is in other apps.

--
Randy
comp.lang.javascript FAQ - http://jibbering.com/faq & newsgroup weekly
Oct 15 '05 #11

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

Similar topics

20
by: Vijay Kumar R. Zanvar | last post by:
Hello, Unlike register, auto keyword can not be used to declare formal parameter(s). Is there any specific reason for this? Kind regards, Vijay Kumar R. Zanvar
2
by: VB Programmer | last post by:
I created a page which I am using to prevent the user from hitting the BACK button. I'll call it my "Auto Jump" page. When it is called it basically auto-redirects to a page specified in the...
7
by: Brian | last post by:
hello, I am looking for a way to auto refresh a web page that I created, but also let the user choose to stop the auto refresh. I can not figure out how to stop the auto refresh. Any help would...
5
by: maya | last post by:
at work they decided to center divs thus: body {text-align:center} #content {width: 612px; text-align:left; margin: 0 auto 0 auto; } this works fine in IE & FF, EXCEPT in FF it doesn't work if...
13
by: S.Dickson | last post by:
I had an access database that i use as an ordering system. I have a form for entering customer details. When i add a new customer on the form the customer number is an auto number that appears when...
9
by: Beowulf | last post by:
I was having this problem: http://groups.google.com/group/microsoft.public.sqlserver.server/msg/e36e423972323378?dmode=source with it taking an inordinate amount of time to enumerate the...
4
by: =?Utf-8?B?TWFyaw==?= | last post by:
Hi... Following the samples online, we implemented a windows service in .Net. Like most services, there's a worker thread actually doing stuff in the background. The problem we've run into is...
22
by: nospam_news | last post by:
I currently get asked about my usage of "auto". What is it for? The keyword is clearly superflous here. In contrast to the huge majority of C/C++ developers I write definitions very explicitly...
1
by: pravinnweb | last post by:
can anyone tell me how to set auto height to outer div that is in green box id "gray-background" it should increase relatively to inner div "smbox" here is the css and html code it should work in...
21
by: JOYCE | last post by:
Look the subject,that's my problem! I hope someone can help me, thanks
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: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
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
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

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.