473,397 Members | 1,985 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,397 software developers and data experts.

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;
Jul 20 '05 #1
17 1544
Phill Long wrote:
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.

--
Randy
Chance Favors The Prepared Mind
comp.lang.javascript FAQ - http://jibbering.com/faq/

Jul 20 '05 #2
Phill Long wrote:
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/
Jul 20 '05 #3
Jerry Park wrote:
Phill Long wrote:
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.

--
Randy
Chance Favors The Prepared Mind
comp.lang.javascript FAQ - http://jibbering.com/faq/

Jul 20 '05 #4
Randy Webb wrote:
Jerry Park wrote:
Phill Long wrote:
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 ...
Jul 20 '05 #5
On Fri, 13 Feb 2004 18:02:20 -0600, Jerry Park <No*****@No.Spam> wrote:
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

--
Michael Winter
M.******@blueyonder.co.invalid (replace ".invalid" with ".uk" to reply)
Jul 20 '05 #6
Michael Winter wrote:
On Fri, 13 Feb 2004 18:02:20 -0600, Jerry Park <No*****@No.Spam> wrote:
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

Its been a long time since I did anything in javascript. You are, of
course, right. Number is no longer an appropriate variable name.
Jul 20 '05 #7
Jerry Park <No*****@No.Spam> writes:
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
--
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 20 '05 #8
Jerry Park wrote:
Randy Webb wrote:
Jerry Park wrote:
Phill Long wrote:


<--snip-->
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.
--
Randy
Chance Favors The Prepared Mind
comp.lang.javascript FAQ - http://jibbering.com/faq/

Jul 20 '05 #9
Michael Winter wrote:
On Fri, 13 Feb 2004 18:02:20 -0600, Jerry Park <No*****@No.Spam> wrote:
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.


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/

Jul 20 '05 #10
Randy Webb <hi************@aol.com> writes:
Michael Winter wrote:
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)
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:
---
function checkItToo(){
alert(Number);
}()
---
It is there inside functions too, unless you hide it.

/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 20 '05 #11
Randy Webb wrote:
Jerry Park wrote:
Randy Webb wrote:
Jerry Park wrote:

Phill Long wrote:

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

Yes. You are correct.
Jul 20 '05 #12
Lasse Reichstein Nielsen wrote:

<--snip-->
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);
}()


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/

Jul 20 '05 #13
"Randy Webb" <hi************@aol.com> wrote in message
news:UK********************@comcast.com...
<snip>
function checkItToo(){
alert(Number);
}()


... 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.
Jul 20 '05 #14
"Richard Cornford" <Ri*****@litotes.demon.co.uk> writes:
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
--
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 20 '05 #15
JRS: In article <Yv*******************@bignews5.bellsouth.net>, seen in
news:comp.lang.javascript, Jerry Park <No*****@No.Spam> posted at Fri,
13 Feb 2004 18:02:20 :-
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.

--
© 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.
Jul 20 '05 #16
> 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/
Jul 20 '05 #17
"Lasse Reichstein Nielsen" <lr*@hotpop.com> wrote in message
news:n0**********@hotpop.com...
<snip>
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?).

<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.
Jul 20 '05 #18

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

Similar topics

16
by: DataBard007 | last post by:
I have an Access97 application whose form contains many text boxes. What do I have to do in order to copy the contents of one of these text boxes to the clipboard? I want to do this so I can then...
2
by: dixie | last post by:
I have a combo box on a form that contains some text of the type "Warning Letter 3" (this is the bound field). I want to take the last character from the contents of this combo box and place it in...
2
by: dixie | last post by:
I have a list box that has a list of ID numbers for people. The box actually shows their names though. I want to take this list and programmatically put it into a text box on the same form in the...
15
by: Paul T. Rong | last post by:
Hi everybody, This time a very very difficult case: I have a table "tblStudent", there are 50 students, I also made a form "frmStudent" based on "tblStudent", now if I don't want to show all...
1
by: Jeremy Chapman | last post by:
How can I display a file open dialog by clicking a button in an aspx page, have the user select a text file then display the contents into a control such as a text box
1
by: mimo | last post by:
Hello, I have seen several examples with a dropdownlist filtering the results in a gridview. Is there a way to have information typed in a text box used to filter a gridview? I would like...
2
by: Keith Wilby | last post by:
A2003, XP Pro. I have a text box on a form. The text box is bound to a hyperlink field. I want to use the contents of the text box in code so I'm assigning the contents to a string variable. ...
8
by: david.lindsay.green | last post by:
Hello all, I am quite new a web scripting and making web pages in general and I have stumbled across a problem I have as yet been unable to solve. I am trying to take the contents of a textarea box...
7
by: Arne Beruldsen | last post by:
in vbnet2005 I have a datagridview. When the user clicks on a row...I would like the contents of certain cells to populate a textbox. To do this...i need to be able to refer to the row and...
1
by: mirandacascade | last post by:
1) Module1 has the following delcaration: Public g_frmZZZ as Form Public g_txtForm2 as Variant 2) app has two forms: form1 and form2 3) a command button on form1 opens form2; it also has...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
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
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
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
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

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.