Add contents of 2 text box's

R

Randy Webb

Phill said:
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;

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.
 
R

Randy Webb

Jerry said:
Phill said:
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;
This link might help:
http://www.aptools.com/javascript/

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.
 
P

Phill Long

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;
 
J

Jerry Park

Phill said:
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;
This link might help:
http://www.aptools.com/javascript/
 
R

Randy Webb

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 ...

True, I guess.
It was written as a drop in library. Doesn't really matter if some other
code is simpler so long as it works.

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.
Don't know what the name of a parameter should matter ...

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.
 
R

Randy Webb

Michael said:
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.



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.

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();
 
R

Randy Webb

Lasse Reichstein Nielsen wrote:

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:

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.
 
J

Jerry Park

Randy said:
Jerry said:
Phill said:
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;
This link might help:
http://www.aptools.com/javascript/


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.
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 ...
 
M

Michael Winter

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 ...

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.
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 ...

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
 
J

Jerry Park

Michael said:
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.



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
Its been a long time since I did anything in javascript. You are, of
course, right. Number is no longer an appropriate variable name.
 
L

Lasse Reichstein Nielsen

Jerry Park said:
Its been a long time since I did anything in javascript. You are, of
course, right. Number is no longer an appropriate variable name.

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
 
L

Lasse Reichstein Nielsen

Randy Webb said:
Michael Winter wrote:
Outside a function, that is true. Inside, it is not (in IE6 anyway)

Yes it is.
....
Inside a function :
function checkIt(){
alert(Number); //undefined

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:
 
J

Jerry Park

Randy said:
True, I guess.



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.



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.
Yes. You are correct.
 
R

Richard Cornford

... the second set of () (what is
the purpose of the second set?). ...
<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.
 
D

Dr John Stockton

JRS: In article <[email protected]>, seen in
It was written as a drop in library. Doesn't really matter if some other
code is simpler so long as it works.

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.
 
L

Lasse Reichstein Nielsen

Richard Cornford said:
The inline execution of the function expression (with optional
identifier). Similar to:-

(function(){ ... })();
....
My experimentation with the technique suggests that IE is happiest if
the function expression is parenthesised, though it should not matter.

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
 
D

Douglas Crockford

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.

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/
 
R

Richard Cornford

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?).
<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.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
474,444
Messages
2,571,709
Members
48,796
Latest member
Greg L.
Top