Add contents of 2 text box's 
July 20th, 2005, 02:30 PM
| | | Add contents of 2 text box's
Right, ok, well I have designed a form that will display a price (22.34) in
a text box, and another price in the other text box... I also have a blank
text box... Now I want to add both the prices together and get a total in
the bloank textbox..
I got this example off a website sumwhere.. But it does not display the
decimal number, it only displays the whole number
PLEASE HELP
var number1 = parseInt(document.forms[0].CasesSellingPrice.value);
var number2 = parseInt(document.forms[0].AccessoriesSellingPrice.value);
document.forms[0].total.value = number1 + number2; | 
July 20th, 2005, 02:30 PM
| | | Re: Add contents of 2 text box's
Phill Long wrote:
[color=blue]
> Right, ok, well I have designed a form that will display a price (22.34) in
> a text box, and another price in the other text box... I also have a blank
> text box... Now I want to add both the prices together and get a total in
> the bloank textbox..
> I got this example off a website sumwhere.. But it does not display the
> decimal number, it only displays the whole number
>
> PLEASE HELP
>
> var number1 = parseInt(document.forms[0].CasesSellingPrice.value);
> var number2 = parseInt(document.forms[0].AccessoriesSellingPrice.value);
> document.forms[0].total.value = number1 + number2;[/color]
Read the FAQ. All of it, but especially http://www.jibbering.com/faq/#FAQ4_21
var number1 = +document.forms[0].CasesSellingPrice.value;
var number2 = +document.forms[0].AccessoriesSellingPrice.value;
document.forms[0].total.value = number1 + number2;
And, why are you using mixed accessors for the form elements? Try to
keep it uniform:
var number1 = +document.forms[0].elements['CasesSellingPrice'].value;
var number2 = +document.forms[0].elements['AccessoriesSellingPrice'].value;
document.forms[0].elements['total'].value = number1 + number2;
Is better but replacing the 0 with the forms name/id would be even better.
--
Randy
Chance Favors The Prepared Mind
comp.lang.javascript FAQ - http://jibbering.com/faq/ | 
July 20th, 2005, 02:30 PM
| | | Re: Add contents of 2 text box's
Phill Long wrote:
[color=blue]
> Right, ok, well I have designed a form that will display a price (22.34) in
> a text box, and another price in the other text box... I also have a blank
> text box... Now I want to add both the prices together and get a total in
> the bloank textbox..
> I got this example off a website sumwhere.. But it does not display the
> decimal number, it only displays the whole number
>
> PLEASE HELP
>
> var number1 = parseInt(document.forms[0].CasesSellingPrice.value);
> var number2 = parseInt(document.forms[0].AccessoriesSellingPrice.value);
> document.forms[0].total.value = number1 + number2;
>
>[/color]
This link might help: http://www.aptools.com/javascript/ | 
July 20th, 2005, 02:31 PM
| | | Re: Add contents of 2 text box's
Jerry Park wrote:
[color=blue]
> Phill Long wrote:
>[color=green]
>> Right, ok, well I have designed a form that will display a price
>> (22.34) in
>> a text box, and another price in the other text box... I also have a
>> blank
>> text box... Now I want to add both the prices together and get a total in
>> the bloank textbox..
>> I got this example off a website sumwhere.. But it does not display the
>> decimal number, it only displays the whole number
>>
>> PLEASE HELP
>>
>> var number1 = parseInt(document.forms[0].CasesSellingPrice.value);
>> var number2 = parseInt(document.forms[0].AccessoriesSellingPrice.value);
>> document.forms[0].total.value = number1 + number2;
>>
>>[/color]
> This link might help:
> http://www.aptools.com/javascript/[/color]
I found it funny actually. Without going through the entire script (its
inherently long), lets go through the first 5 or 6 significant lines:
<script language="javascript">
Should be type="text/javascript", but since it appears to have been last
modified in 2001, its not relevant.
<!-- Hide from browsers without javascript.
Nor is the HTML comment entity needed. I guess if you browse with a 10
year old browser it might be needed.
function FormatNumber(Number,Decimals,Separator)
{
Not sure that I would want a variable (parameter) named Number.
NumToFormat seems better and more intuitive.
// ************************************************** ********
// Placed in the public domain by Affordable Production Tools
// March 21, 1998
// Web site: http://www.aptools.com/
//
// November 24, 1998 -- Error which allowed a null value
// to remain null fixed. Now forces value to 0.
//
// October 28, 2001 -- Modified to provide leading 0 for fractional number
// less than 1.
//
// This function accepts a number to format and number
// specifying the number of decimal places to format to. May
// optionally use a separator other than '.' if specified.
//
// If no decimals are specified, the function defaults to
// two decimal places. If no number is passed, the function
// defaults to 0. Decimal separator defaults to '.' .
//
// If the number passed is too large to format as a decimal
// number (e.g.: 1.23e+25), or if the conversion process
// results in such a number, the original number is returned
// unchanged.
// ************************************************** ********
Number += "" // Force argument to string.
Decimals += "" // Force argument to string.
Separator += "" // Force argument to string.
Decimals = Decimals.toString() perhaps? Might be buggy though, I don't
remember. http://www.jibbering.com/faq/#FAQ4_6
Seems to be a heck of a lot shorter (although it lacks some of the
functionality in the above) and a lot easier to follow.
None of which answers/addresses the OP's original problem. See my
previous reply in this thread.
--
Randy
Chance Favors The Prepared Mind
comp.lang.javascript FAQ - http://jibbering.com/faq/ | 
July 20th, 2005, 02:31 PM
| | | Re: Add contents of 2 text box's
Randy Webb wrote:[color=blue]
> Jerry Park wrote:
>[color=green]
>> Phill Long wrote:
>>[color=darkred]
>>> Right, ok, well I have designed a form that will display a price
>>> (22.34) in
>>> a text box, and another price in the other text box... I also have a
>>> blank
>>> text box... Now I want to add both the prices together and get a
>>> total in
>>> the bloank textbox..
>>> I got this example off a website sumwhere.. But it does not display the
>>> decimal number, it only displays the whole number
>>>
>>> PLEASE HELP
>>>
>>> var number1 = parseInt(document.forms[0].CasesSellingPrice.value);
>>> var number2 = parseInt(document.forms[0].AccessoriesSellingPrice.value);
>>> document.forms[0].total.value = number1 + number2;
>>>
>>>[/color]
>> This link might help:
>> http://www.aptools.com/javascript/[/color]
>
>
> I found it funny actually. Without going through the entire script (its
> inherently long), lets go through the first 5 or 6 significant lines:
>
>
> <script language="javascript">
>
> Should be type="text/javascript", but since it appears to have been last
> modified in 2001, its not relevant.
>
> <!-- Hide from browsers without javascript.
>
> Nor is the HTML comment entity needed. I guess if you browse with a 10
> year old browser it might be needed.
>
> function FormatNumber(Number,Decimals,Separator)
> {
>
> Not sure that I would want a variable (parameter) named Number.
> NumToFormat seems better and more intuitive.
>
> // ************************************************** ********
> // Placed in the public domain by Affordable Production Tools
> // March 21, 1998
> // Web site: http://www.aptools.com/
> //
> // November 24, 1998 -- Error which allowed a null value
> // to remain null fixed. Now forces value to 0.
> //
> // October 28, 2001 -- Modified to provide leading 0 for fractional number
> // less than 1.
> //
> // This function accepts a number to format and number
> // specifying the number of decimal places to format to. May
> // optionally use a separator other than '.' if specified.
> //
> // If no decimals are specified, the function defaults to
> // two decimal places. If no number is passed, the function
> // defaults to 0. Decimal separator defaults to '.' .
> //
> // If the number passed is too large to format as a decimal
> // number (e.g.: 1.23e+25), or if the conversion process
> // results in such a number, the original number is returned
> // unchanged.
> // ************************************************** ********
> Number += "" // Force argument to string.
> Decimals += "" // Force argument to string.
> Separator += "" // Force argument to string.
>
> Decimals = Decimals.toString() perhaps? Might be buggy though, I don't
> remember.
>
>
> http://www.jibbering.com/faq/#FAQ4_6
>
> Seems to be a heck of a lot shorter (although it lacks some of the
> functionality in the above) and a lot easier to follow.
>
> None of which answers/addresses the OP's original problem. See my
> previous reply in this thread.
>[/color]
Thanks for the comments. Yes, it was written for older browsers, but
still works for current browsers. There are still a lot of older
browsers around ...
It was written as a drop in library. Doesn't really matter if some other
code is simpler so long as it works.
Don't know what the name of a parameter should matter ... | 
July 20th, 2005, 02:31 PM
| | | Re: Add contents of 2 text box's
On Fri, 13 Feb 2004 18:02:20 -0600, Jerry Park <NoReply@No.Spam> wrote:
[color=blue]
> Thanks for the comments. Yes, it was written for older browsers, but
> still works for current browsers. There are still a lot of older
> browsers around ...[/color]
The reason why developers needed to comment out the contents of script
elements was because browsers that didn't understand the script element
would render the contents. I very much doubt there are any browsers in use
now that don't have knowledge of the script element, making that practice
obsolete. In XHTML, it is actually destructive.
[color=blue]
> It was written as a drop in library. Doesn't really matter if some other
> code is simpler so long as it works.
>
> Don't know what the name of a parameter should matter ...[/color]
The identifier, Number, is a global function and an object used to convert
and represent numerical values, respectively. By using that identifier,
you hide those built-in features.
Mike
--
Michael Winter M.Winter@blueyonder.co.invalid (replace ".invalid" with ".uk" to reply) | 
July 20th, 2005, 02:31 PM
| | | Re: Add contents of 2 text box's
Michael Winter wrote:[color=blue]
> On Fri, 13 Feb 2004 18:02:20 -0600, Jerry Park <NoReply@No.Spam> wrote:
>[color=green]
>> Thanks for the comments. Yes, it was written for older browsers, but
>> still works for current browsers. There are still a lot of older
>> browsers around ...[/color]
>
>
> The reason why developers needed to comment out the contents of script
> elements was because browsers that didn't understand the script element
> would render the contents. I very much doubt there are any browsers in
> use now that don't have knowledge of the script element, making that
> practice obsolete. In XHTML, it is actually destructive.
>[color=green]
>> It was written as a drop in library. Doesn't really matter if some
>> other code is simpler so long as it works.
>>
>> Don't know what the name of a parameter should matter ...[/color]
>
>
> The identifier, Number, is a global function and an object used to
> convert and represent numerical values, respectively. By using that
> identifier, you hide those built-in features.
>
> Mike
>[/color]
Its been a long time since I did anything in javascript. You are, of
course, right. Number is no longer an appropriate variable name. | 
July 20th, 2005, 02:31 PM
| | | Re: Add contents of 2 text box's
Jerry Park <NoReply@No.Spam> writes:
[color=blue]
> Its been a long time since I did anything in javascript. You are, of
> course, right. Number is no longer an appropriate variable name.[/color]
It never were. The Number function was defined in Netscape 2, the first
Javascript browser at all. I think it is safe to assume that there
is no Javascript capable browser without it.
It's only a stylistic problen, so it's not really a problem unless
somebody else needs to read the code, ofcourse :)
/L
--
Lasse Reichstein Nielsen - lrn@hotpop.com
DHTML Death Colors: <URL:http://www.infimum.dk/HTML/rasterTriangleDOM.html>
'Faith without judgement merely degrades the spirit divine.' | 
July 20th, 2005, 02:31 PM
| | | Re: Add contents of 2 text box's
Jerry Park wrote:[color=blue]
> Randy Webb wrote:
>[color=green]
>> Jerry Park wrote:
>>[color=darkred]
>>> Phill Long wrote:
>>>[/color][/color][/color]
<--snip-->
[color=blue]
> Thanks for the comments. Yes, it was written for older browsers, but
> still works for current browsers. There are still a lot of older
> browsers around ...[/color]
True, I guess.
[color=blue]
> It was written as a drop in library. Doesn't really matter if some other
> code is simpler so long as it works.[/color]
Actually, I disagree with that. Just because code "works" doesn't make
it optimal. It is often when you try to write a "catch all" function
that you end up bloating it (due to trying to cover everything) when a
simple solution will suffice.
[color=blue]
> Don't know what the name of a parameter should matter ...[/color]
alert(Number)
var Number= 1;
alert(Number)
Try that in your browser of choice and it will soon become very apparent
why it should matter. And when you start the bad habit of using a
reserved word inside a function, it will invariably creep outside your
function and then you run into issues as above.
--
Randy
Chance Favors The Prepared Mind
comp.lang.javascript FAQ - http://jibbering.com/faq/ | 
July 20th, 2005, 02:31 PM
| | | Re: Add contents of 2 text box's
Michael Winter wrote:
[color=blue]
> On Fri, 13 Feb 2004 18:02:20 -0600, Jerry Park <NoReply@No.Spam> wrote:
>[color=green]
>> Thanks for the comments. Yes, it was written for older browsers, but
>> still works for current browsers. There are still a lot of older
>> browsers around ...[/color]
>
>
> The reason why developers needed to comment out the contents of script
> elements was because browsers that didn't understand the script element
> would render the contents. I very much doubt there are any browsers in
> use now that don't have knowledge of the script element, making that
> practice obsolete. In XHTML, it is actually destructive.
>[color=green]
>> It was written as a drop in library. Doesn't really matter if some
>> other code is simpler so long as it works.
>>
>> Don't know what the name of a parameter should matter ...[/color]
>
>
> The identifier, Number, is a global function and an object used to
> convert and represent numerical values, respectively. By using that
> identifier, you hide those built-in features.[/color]
Outside a function, that is true. Inside, it is not (in IE6 anyway)
Outside a function:
alert(Number); //alerts function Number(){ [native code] }
var Number= 1;
alert(Number); //alerts 1
Inside a function :
function checkIt(){
alert(Number); //undefined
var Number= 1;
alert(Number); // 1
}
checkit();
--
Randy
Chance Favors The Prepared Mind
comp.lang.javascript FAQ - http://jibbering.com/faq/ | 
July 20th, 2005, 02:31 PM
| | | Re: Add contents of 2 text box's
Randy Webb <hikksnotathome@aol.com> writes:
[color=blue]
>Michael Winter wrote:[/color]
[color=blue][color=green]
>> The identifier, Number, is a global function and an object used to
>> convert and represent numerical values, respectively. By using that
>> identifier, you hide those built-in features.[/color][/color]
[color=blue]
> Outside a function, that is true. Inside, it is not (in IE6 anyway)[/color]
Yes it is.
....[color=blue]
> Inside a function :
> function checkIt(){
> alert(Number); //undefined[/color]
So, you have already hidden it here.
The "var Number" declaration makes "Number" in this scope refer to
the local variable, which does hide the global variable. It does that
for the entire scope (the function body), including the part before
the declaration.
Try this:
---
function checkItToo(){
alert(Number);
}()
---
It is there inside functions too, unless you hide it.
/L
--
Lasse Reichstein Nielsen - lrn@hotpop.com
DHTML Death Colors: <URL:http://www.infimum.dk/HTML/rasterTriangleDOM.html>
'Faith without judgement merely degrades the spirit divine.' | 
July 20th, 2005, 02:31 PM
| | | Re: Add contents of 2 text box's
Randy Webb wrote:[color=blue]
> Jerry Park wrote:
>[color=green]
>> Randy Webb wrote:
>>[color=darkred]
>>> Jerry Park wrote:
>>>
>>>> Phill Long wrote:
>>>>[/color][/color]
>
>
> <--snip-->
>[color=green]
>> Thanks for the comments. Yes, it was written for older browsers, but
>> still works for current browsers. There are still a lot of older
>> browsers around ...[/color]
>
>
> True, I guess.
>[color=green]
>> It was written as a drop in library. Doesn't really matter if some
>> other code is simpler so long as it works.[/color]
>
>
> Actually, I disagree with that. Just because code "works" doesn't make
> it optimal. It is often when you try to write a "catch all" function
> that you end up bloating it (due to trying to cover everything) when a
> simple solution will suffice.
>[color=green]
>> Don't know what the name of a parameter should matter ...[/color]
>
>
> alert(Number)
> var Number= 1;
> alert(Number)
>
> Try that in your browser of choice and it will soon become very apparent
> why it should matter. And when you start the bad habit of using a
> reserved word inside a function, it will invariably creep outside your
> function and then you run into issues as above.
>
>[/color]
Yes. You are correct. | 
July 20th, 2005, 02:31 PM
| | | Re: Add contents of 2 text box's
Lasse Reichstein Nielsen wrote:
<--snip-->
[color=blue]
> The "var Number" declaration makes "Number" in this scope refer to
> the local variable, which does hide the global variable. It does that
> for the entire scope (the function body), including the part before
> the declaration.
>
> Try this:
> ---
> function checkItToo(){
> alert(Number);
> }()[/color]
In IE6, it balks with an error pointing at the second set of () (what is
the purpose of the second set?). When removed, no syntax error. I
thought (wrongly so), that I had used just the function (without the
outside definitions) but I guess I had it all in there. Ooops.
Even with my testing error, its still a very very bad variable name, as
is any reserved word. I don't even like using different capitalizations
of them: numBer , to me, is just as bad.
--
Randy
Chance Favors The Prepared Mind
comp.lang.javascript FAQ - http://jibbering.com/faq/ | 
July 20th, 2005, 02:31 PM
| | | Re: Add contents of 2 text box's
"Randy Webb" <hikksnotathome@aol.com> wrote in message
news:UKOdncvfjqegErDdRVn-uw@comcast.com...
<snip>[color=blue][color=green]
>>function checkItToo(){
>> alert(Number);
>>}()[/color]
>
>... the second set of () (what is
> the purpose of the second set?). ...[/color]
<snip>
The inline execution of the function expression (with optional
identifier). Similar to:-
(function(){ ... })();
- which uses an anonymous function expression. The content of the first
set of parentheses are evaluated as a reference to an (anonymous)
function object and then the final - () - calls it. In exactly the way
the identifier for a function is evaluated as a reference to a function
object and a following - () - calls it.
The first time I noticed this used it was in one of Yep's ingenious
scripts. It has proved extremely useful and can allow quite complex
scripts to execute totally anonymously without impacting on the global
namespace at all.
My experimentation with the technique suggests that IE is happiest if
the function expression is parenthesised, though it should not matter.
Richard. | 
July 20th, 2005, 02:31 PM
| | | Re: Add contents of 2 text box's
"Richard Cornford" <Richard@litotes.demon.co.uk> writes:
[color=blue]
> The inline execution of the function expression (with optional
> identifier). Similar to:-
>
> (function(){ ... })();[/color]
....[color=blue]
> My experimentation with the technique suggests that IE is happiest if
> the function expression is parenthesised, though it should not matter.[/color]
Yes, I had omited the wrapping parentheses because I thought it didn't
matter. Well, live and learn. According to ECMA 262, an
ExpressionStatement may not begin with "function" or "{" (most likely
to avoid the ambiguity between function declarations and function
expressions with the optional identifier, as well as between object
literals and BlockStatements - is "{x:42}" an object literal or
a block with a label and an ExpressionStatement?).
So, my code was incorrect when used alone.
/L
--
Lasse Reichstein Nielsen - lrn@hotpop.com
DHTML Death Colors: <URL:http://www.infimum.dk/HTML/rasterTriangleDOM.html>
'Faith without judgement merely degrades the spirit divine.' | 
July 20th, 2005, 02:31 PM
| | | Re: Add contents of 2 text box's
JRS: In article <YvdXb.34120$A75.27987@bignews5.bellsouth.net>, seen in
news:comp.lang.javascript, Jerry Park <NoReply@No.Spam> posted at Fri,
13 Feb 2004 18:02:20 :-
[color=blue]
>It was written as a drop in library. Doesn't really matter if some other
>code is simpler so long as it works.[/color]
It does matter. Simpler code (as opposed to code written by the simple)
is generally shorter and often quicker.
In a good compiled language, only those parts needed from a library will
be present in the delivered product, and therefore comment and
alternatives can be extensive.
With javascript, however, the entire source is delivered to each user,
and this delivery may be over a limited-bandwidth and/or charged-by-time
link.
In a javascript library, therefore, comment should be easy to remove
(that's easy to arrange); and monolithic multifunctional units, where
any given use is liable only to need a small part of the functionality,
should be avoided.
OP, etc.:
The plural of box is boxes, not box's. We are not greengrocers.
--
© John Stockton, Surrey, UK. ?@merlyn.demon.co.uk Turnpike v4.00 IE 4 ©
<URL:http://jibbering.com/faq/> Jim Ley's FAQ for 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. | 
July 20th, 2005, 02:31 PM
| | | Re: Add contents of 2 text box's
> The inline execution of the function expression (with optional[color=blue]
> identifier). Similar to:-
>
> (function(){ ... })();
>
> - which uses an anonymous function expression. The content of the first
> set of parentheses are evaluated as a reference to an (anonymous)
> function object and then the final - () - calls it. In exactly the way
> the identifier for a function is evaluated as a reference to a function
> object and a following - () - calls it.
>
> The first time I noticed this used it was in one of Yep's ingenious
> scripts. It has proved extremely useful and can allow quite complex
> scripts to execute totally anonymously without impacting on the global
> namespace at all.
>
> My experimentation with the technique suggests that IE is happiest if
> the function expression is parenthesised, though it should not matter.[/color]
It does matter because syntactically 'function' is a statement. The
outer parens cause it so be read as an expression. The ECMAScript
standard could have permitted function statements to have an invocation
part, but it doesn't. I once requested that it do, but that is not
likely to happen. http://www.ecmascript.org/ | 
July 20th, 2005, 02:35 PM
| | | Re: Add contents of 2 text box's
"Lasse Reichstein Nielsen" <lrn@hotpop.com> wrote in message
news:n07l6axv.fsf@hotpop.com...
<snip>[color=blue][color=green]
>>My experimentation with the technique suggests that IE is happiest if
>>the function expression is parenthesised, though it should not matter.[/color]
>
>Yes, I had omited the wrapping parentheses because I thought it didn't
>matter. Well, live and learn. According to ECMA 262, an
>ExpressionStatement may not begin with "function" or "{" (most likely
>to avoid the ambiguity between function declarations and function
>expressions with the optional identifier, as well as between object
>literals and BlockStatements - is "{x:42}" an object literal or
>a block with a label and an ExpressionStatement?).[/color]
<snip>
That is fair enough in the context of the previous example but I also
noticed IE having problems with:-
var something = function(){
var global = this;
...;
return (function()( ... )};
}();
Where the - this - reference was failing to refer to the global object
(or apparently any object) unless the function expression was
parenthesised. In that case the function expression is not ambiguous, or
any more so that assigning a reference to a function object with the
assignment of a function expression.
I don't recall if this was a consistent problem I just recall that it
the second time I noticed it I resolved to always parenthesise my
function expressions.
Richard. | | Thread Tools | Search this Thread | | | |
Posting Rules
| You may not post new threads You may not post replies You may not post attachments You may not edit your posts HTML code is Off | | | | | | What is Bytes?
We are a network of experts and professionals in IT and software development that help one another with answers to tough questions and share insights.
Get the best answers to your questions from over 220,989 network members.
|